Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - what goes into the form action parameter when view requires a parameter?

This is what I have:

myview.py with a view that takes a parameter user:

def myview(request, user):    form = MyForm(request.POST)    ....    return render_to_response('template.html',locals(), context_instance=RequestContext(request)) 

The user gets passed through an url.

urls.py:

...  urlpatterns += patterns('myview.views',     (r'^(?P<user>\w+)/', 'myview'), )  ... 

I also have a template.html with a form:

<form name="form" method="post" action="."> ... </form> 

What goes in the the form action parameter if myview function requires a parameter?

Right now I have action="." . The reason I'm asking is because when I fill up the form In (templates.html) and click the submit button I see absolutely no field values passed from that form. It's almost like I'm passing an empty form when I click the submit button. Any ideas? Thank you!

like image 454
avatar Avatar asked Mar 29 '11 02:03

avatar


People also ask

How do you receive data from a Django form with a post request?

Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.


1 Answers

If you want to explicitly set the action, assuming you have a variable username in your template,

<form name="form" method="post" action="{% url myview.views username %}"> 

or you could assign a name for the url in your urls.py so you could reference it like this:

# urls.py urlpatterns += patterns('myview.views',     url(r'^(?P<user>\w+)/', 'myview', name='myurl'), # I can't think of a better name )  # template.html <form name="form" method="post" action="{% url myurl username %}"> 
like image 98
gladysbixly Avatar answered Sep 22 '22 20:09

gladysbixly