Friday, August 26, 2016

Re: How to set up a form from an existing view

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 :)

2016-08-26 16:00 GMT+02:00 Aaron Weisberg <aaron.m.weisberg@gmail.com>:
> 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.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
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/CAEuG%2BTZtE_QP3uq473HkW%2BRmYNtPrHV3XO%2B09PubC4sp5k-H0A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment