My views.py:
@login_required
def some_views(request):
if request.method == 'POST':
form = AddressCreateFrom(request.POST)
if form.is_valid():
name = form.cleaned_data['Address']
ip_value = form.cleaned_data['value']
user_list = get_username(name)
address_create = form.save()
extra_context = {
'user_list': user_list
}
return redirect_to(request, url=address_create.get_absolute_url())
else:
form = AddressCreateFrom()
extra_context = {
'form':AddressCreateFrom(initial={'user': request.user.pk})
}
return direct_to_template(request,'networks/user_form.html',extra_context)
In form.py:
class AddressCreateFrom(forms.ModelForm):
Address = forms.CharField(max_length=40)
value = forms.CharField(max_length=40)
class Meta:
model = Network
widgets = {
'user': forms.HiddenInput()
}
As you see that i am using Django model form with two extra Django form field i.e. Address and value in AddressCreateForm class. I need all of the field at the time of rendering the template.
Indeed some_views method are working fine but i also want render some extra data written in context_dictionary i.e. user_list to a requesting URL i.e. address_create.get_absolute_url().
If i am not wrong, if we are handling with the database we have to use redirect_to method. Is it possible to do that?
A redirect will return a HTTP response with status code 301 or 302, and the location to redirect to:
There is no template rendered by the original view, so you can’t pass
extra_contextto it.The user’s browser will usually follow the redirect, and request the new URL.
If you want to display information about a particular user in the next view, you have to do something like:
/users/200/,/users/?id=200, then fetch the user id fromrequest.GETin the view.Then in the view that you redirect to, you can fetch the user from the database, and add it to the template context.