Cant get page to redirect with Django - python

def add(request):
if request.method == "POST":
form = ContactForm(request.POST)
if form.is_valid():
title = form.cleaned_data["title"]
content = form.cleaned_data["content"]
return HttpResponseRedirect(reverse("index",args=(title,content, {
"entries": util.save_entry(title, content)
})))
else:
return render(request, "encyclopedia/add.html",{
"form": form
})
else:
return render(request, "encyclopedia/add.html",{
"form": ContactForm()
})
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
I cant seem to get the redirecting to work. Everything else works up till there please help. Below are my code and error
views.py
error
urls.py

Related

Get url param in django

I have this view:
#login_required
def newAnswer(request, id):
post = Post.objects.get(id=id)
form = AnswerForm(request.POST)
if request.method == 'POST':
if form.is_valid():
obj = form.save(commit=False)
obj.author = request.user
obj.post = post
obj.save()
form.save_m2m()
return redirect('main:post', id=post.id)
else:
return render(request, 'main/newAnswer.html', { 'form': form, 'formErrors': form.errors, 'userAvatar': getAvatar(request.user)})
else:
return render(request, 'main/newAnswer.html', {'form': form, 'post': post, 'userAvatar': getAvatar(request.user)})
When i try to post without loging in, it redirects me to "/accounts/login?next=/post/answer/new/81".
My question is how can i get the "next" param in my login view
thanks!
Everything arguments (params) you can see in url mean that request is done with GET method. Use request.GET.get('next', None).

How can I get the primary key of the last insered quote?

I have 2 views: display_quote and quote_create.
def display_quote(request, pk):
items_quote = Quote.objects.filter(pk=pk)
items_quote_line = LineQuote.objects.all().filter(num_quote_id=pk)
form = QuoteLineForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
form.save()
total = 0
for item in items_quote_line:
total = total + item.get_price()
context = {'items_quote': items_quote,
'items_quote_line': items_quote_line,
'form': form,
'total':total
}
return render(request, 'quote/quote_display.html', context)
def quote_create(request):
form = QuoteForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
form.save()
return render(request, 'quote/quote_create.html', {'form': form})
After creating a quote, I want to redirect to "display_quote" (the quote creted).
Error obtained:
The view quote.views.quote_create didn't return an HttpResponse object. It returned None instead.
def quote_create(request):
form = QuoteForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
form.save()
return render(request, 'quote/quote_create.html', {'form': form})
return render(request, "...", {})
return something by default. Else conditions are not handled here...

Using Value From One View in Another View Django

In View 1's form their is a field called 'reference'. I need to access whatever value is submitted in that field in View 2 and set a variable equal to it. Right now I am just getting an error "orders matching query does not exist".
This is what I'm trying (I've commented the code in view2 to indicate where im getting the error).
views.py
def view1(request, pk):
item = get_object_or_404(Manifests, pk=pk)
if request.method == "POST":
form = CreateManifestForm(request.POST, instance=item)
if form.is_valid():
form.save()
return redirect('view2')
else:
form = CreateManifestForm(instance=item)
return render(request, 'edit_manifest_frombrowse.html', {'form': form})
def view2(request):
form = CreateManifestForm(request.POST)
if request.method == "POST":
if form.is_valid():
form.save()
...
reference_id = request.POST.get('reference') #this is how Im trying to get reference from the previos view
data = Manifests.objects.all().filter(reference__reference=reference_id)
form = CreateManifestForm(initial={
'reference': Orders.objects.get(reference=reference_id), #this is where im getting "does not exist"
})
total_cases = Manifests.objects.filter(reference__reference=reference_id).aggregate(Sum('cases'))
context = {
'reference_id': reference_id,
'form': form,
'data': data,
'total_cases': total_cases['cases__sum'],
}
return render(request, 'manifest_readonly.html', context)
forms.py
class CreateManifestForm(forms.ModelForm):
class Meta:
model = Manifests
fields = ('reference', 'cases', 'product_name', 'count', 'CNF', 'FOB')
I just want to be able to use whatever value is submitted in the 'reference' field in view1 in view2 and assign it equal to reference_id
Something like this:
from django.urls import reverse
def view1(request, pk):
item = get_object_or_404(Manifests, pk=pk)
if request.method == "POST":
form = CreateManifestForm(request.POST, instance=item)
if form.is_valid():
obj = form.save()
reference_id = request.POST.get('reference') or obj.reference.id
return redirect(reverse('view2')+f'?reference={reference_id}')
else:
form = CreateManifestForm(instance=item)
return render(request, 'edit_manifest_frombrowse.html', {'form': form})
def view2(request):
if request.method == "POST":
form = CreateManifestForm(request.POST)
if form.is_valid():
form.save()
...
data = getattr(request, 'POST', None) or getattr(request, 'GET', {})
reference_id = data.get('reference') #this is how Im trying to get reference from the previos view
data = Manifests.objects.all().filter(reference__reference=reference_id)
form = CreateManifestForm(initial={
'reference': Orders.objects.get(reference=reference_id), #this is where im getting "does not exist"
})
total_cases = Manifests.objects.filter(reference__reference=reference_id).aggregate(Sum('cases'))
context = {
'reference_id': reference_id,
'form': form,
'data': data,
'total_cases': total_cases['cases__sum'],
}
return render(request, 'manifest_readonly.html', context)

Initial value of inputs in Django not working

I defined an initial value to my title field, but once I execute it, it shows no error and the initial value is not showing in my field how do I solve it? Thanks
def form_view(request):
if request.method == "POST":
initial_data = {
'title': 'default title'
}
form = ProductForm(request.POST, initial=initial_data)
if form.is_valid():
form.save()
form = ProductForm()
else:
form = ProductForm()
return render(request, "form.html", {"form": form})
You've put it in the wrong place. If you want it to show when the form is first rendered, you shouldn't put it in the POST block - which is called when the form is submitted - but the else block.
if request.method == 'POST':
form = ProductForm(request.POST)
...
else:
form = ProductForm(initial=initial_data)

not getting user id after changing password

I am changing the password if the current user is logged in.
views.py:
def change_password(request,pk=None):
user = MyUser.objects.get(pk=pk)
if request.method == "POST":
form = PasswordChangeForm(user=user, data=request.POST)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
context = {
'form': form,
'title': _('Password change'),
}
return render(request, "nurse/change_password.html")
else:
form = PasswordChangeForm(user=user)
context = {
'form': form,
'title': _('Password change'),
}
return render(request,"nurse/change_password.html")
The function is working fine, but after this function call, I am not getting user.id in my html file.
What am I doing wrong?

Categories