Friday, August 26, 2016

How to set up a form from an existing view

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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment