How to set up a form from an existing view

20 views
Skip to first unread message

Aaron Weisberg

unread,
Aug 26, 2016, 10:00:09 AM8/26/16
to Django users
Good morning,

I have been working on an app displaying some analytics and I want to incorporate some user feedback and I am having trouble figuring out how:

I have a detail view:

Enter code here...

#views.py
def detail(request, game_id):
    games = Game.objects.get(pk=game_id)
    context = {
        'games':games}
    return render(request,'myapp/detail.html',context)

Very simple with no form activity as of yet.

My template currently shows a comparison between two properties set up like:

#detail.html

<div class="container">
    <div class="row">
        <div class="col-xs-12 col-sm-offset-4 col-sm-8">
            <div class="row">
                <div class="col-xs-6 my_planHeader my_plan1">
                    <div class="my_planTitle">{{ games.roadTeam }}</div>
                    <div class="my_planPrice">{{ games.roadTeamWins}} - {{ games.roadTeamLosses}}</div>         
                                                     
                       
                </div>
                <div class="col-xs-6 my_planHeader my_plan2">
                    <div class="my_planTitle">{{games.homeTeam }}</div>
                    <div class="my_planPrice">{{ games.HomeTeamWins}} - {{ games.HomeTeamLosses}}</div>
                 
                    
            </div>


I would like to work backwards and turn this template into a form.  Would I be able to make it a form by adding a third div to each one of these that is a button which sends the value of the variable back to the view?  and then how would I be able to process that in the view?
Enter code here...

<a type="button" class="btn btn-default">
                    <form action="{% url 'myapp:detail' %}" method="post">{% csrf_token %}
                    <label for="{% url 'myapp:detail' %}"></label>
                    <input type="submit" value="{{games.roadTeam}}" style="width 100%">
                    </form></a>

I am not an expert when it comes to forms, so your assistance would be greatly appreciated.

Thanks.

ludovic coues

unread,
Aug 26, 2016, 11:33:35 AM8/26/16
to django...@googlegroups.com
First, your html is invalid. You can't put a whole form in a link. Nor
a button. You can remove the <a> element and put the shiny class on
the input[type=submit]

About your input[type=submit], I have some doubts about cross brother
compatibility of what you are trying to achieve. It would be either to
add an input[type=hidden] which the value {{games.roadTeam}}. This
will prevent you some headache if your code don't work under IE or Fx
or chrome or whatnot.

About your question,
you can use the request argument, like this:

if request.method == "POST":
request.POST.get("variable", "default_value")

If you want to do something more complex than simply find which team
is chosen, you should create a form class. This will simplify data
handling.

That being said, you are on a good start :)
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/0569cc00-30c1-4b14-b521-107e37149340%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Aaron Weisberg

unread,
Aug 27, 2016, 8:09:26 AM8/27/16
to django...@googlegroups.com

Thanks ludovic

I'll try that. I have a very basic form question. What attribute gets posted in the .POST? Is that value? In input?

Secondly, the default value in the function, is that a type like text or integer or is it just a place holder like "Chicago" or "2"

Thanks



> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/0569cc00-30c1-4b14-b521-107e37149340%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/2pxh8Pmrfrg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ludovic coues

unread,
Aug 27, 2016, 8:24:35 AM8/27/16
to django...@googlegroups.com
POST is a dictionnary, with all the input there was in the form which
contain the submit button that was clicked. The key will be the name
of the input and the value will be the input value. For exemple, with
this html:

<form><ul>
<li><input type=number name=qty>
<li><input type=submit>
</ul><input type=hidden name=token value=12>
</form>

<form>
<input type=hidden name=token value=15>
<input type=submit>
</form>

If the user click on the first submit, request.POST will have both
token with value 12 and qty. If they click the second submit, only
token with value 15. Different form produce different POST request.

The value of qty will be a string which should not fail when passed to
float(). Django's form class would take care of validating and
converting value but simple HTML form will only pass string, with some
exception.

The method `get` isn't specific to django. It's a object on python
dictionnary. It try to read the first argument, the key. If it fail,
it will return the second argument. both of the following snippet
should act the same:

token = request.POST.get('token', None)

try:
token = request.POST['token']
except KeyError:
token = None
>> > email to django-users...@googlegroups.com.
>> > To post to this group, send email to django...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/django-users.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/django-users/0569cc00-30c1-4b14-b521-107e37149340%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>>
>> Cordialement, Coues Ludovic
>> +336 148 743 42
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/2pxh8Pmrfrg/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAL5RJ57pYTyBsgPmzvC8ryq8jWW-r6PsGTLL7cu8P8iMt7Y0ag%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages