Based on Django URL pass reference FORM form pass data get post usage example

  • 2020-10-07 18:46:13
  • OfStack

POST and GET are common forms interaction methods in THE development of web, and they are the backbone for the construction of the front and back end interaction system of web. Now the simple usage examples in Django are recorded for the reference of subsequent queries and other students

1. URL refs


# The front end html Template tags are used to label the parameters "x" Passed to the achievement The application of yearcontent methods 
<td><a href={% url 'achievement:yearcontent' x %} >{{ x }}</a></td>
#urls.py Receive parameters with regular matching ,(?P<year>[0-9]+) Enclosed in parentheses is the incoming message from the receiving front end x=2017, Good to pass to the view function views.py use ,?P The name used to set the matching part ,views.py I'm going to use that name to receive arguments ,[0-9]+ Indicates that the regular matches all integers 
url(r'^yearcontent/(?P<year>[0-9]+)/$', views.yearcontent, name='yearcontent'),
#views.py The inside receives the incoming year=2017, The body of the subsequent method then does something with the parameters passed in , This is to use URL So it's going to be the vector GET methods 1 Kind of use 
def yearcontent(request,year):

2.FORM forms use POST


# The front end html In the set name, Because then you have to pass name Get the data 
<form class="form-horizontal" role="form" action="{% url 'achievement:set_new_purpose' %}" method="post">
{% csrf_token %}
<div class="form-group">
  <label class="col-lg-2 control-label"> The target </label>
  <div class="col-lg-10">
   <input type="text" class="form-control" id="cc" placeholder="" name="achievement_title">
  </div>
</div>
<div class="form-group">
  <label class="col-lg-2 control-label"> note </label>
  <div class="col-lg-10">
   <textarea id="" class="form-control" cols="30" rows="10" name="achievement_text"></textarea>
  </div>
</div>
<div class="form-group">
  <div class="col-lg-offset-10">
   <button type="submit" class="btn btn-send" type="submit"> determine </button>
  </div>
</div>
</form>
#urls In the 
url(r'^set_year_summary$', views.set_year_summary, name='set_year_summary'),
# The view function views.py In the , request.POST[name] To receive the form Form data , Then you can do things 
def set_year_summary(request):
 achievement_date = request.POST['year']
 achievement_summary = request.POST['achievement_text']
 twz = AchievementYear.objects.get(achievement_date=achievement_date)
 print twz
 twz.achievement_summary = achievement_summary
 twz.save()
 return redirect('achievement:index')

Related articles: