Firstly we create a new app and then we are going to learn something about parameter transferring.
Functions in views.py can take more than one parameter, though, they have to be named, whose name will be used in HTML templates, URLs and URL configure files.
take this function for example.
from django.http import HttpResponse
def index(request, count):
return HttpResponse(count)
This 'count' should also appear in its path in urls.py where its type should also by clarified.
In paths, the parameter should be written like 'blahblah/<int:count>', just like the format function but {s are taken place by <s.
This can also work in a template's tags' srcs or hrefs.
For example,
<a href="{% url 'urlname' parameter %]">hahaha</a>
From here we can see the importance of a urlname which is assigned in paths, like
urlpatterns = [
path('<int:parameter>/index', views.index, name='index'),
If you have more than one app in your project, you may want to namespace your urlpatterns. You can just add app_name in urls.py, and when it's been included the HTML templates' soft-coded url can use it like below.
<a href="{% url 'app_name:urlname' parameter %}">heiheihei</a>
------------------------------------------------------------------------------------------------------------------------------------------------------------
#rendering a template
from django.shortcuts import render
...
return render(request,'appname/iddd.html',{addtional_parameter_dict_used_by_html_template_engine})
...
Django will look up the app's "template" directory and find the path you gave it, and then pass the parameter dict to that html template and return it as if it's a HttpResponse.
use {{ obj_name }} to show the object in a html template.
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
And like Python, there is also something like "for" loop or "if else endif", which are being translated later into HTML.
In the end we are about to talk some heads-up when it comes to databases.
Logically, when you are accessing a webpage whose back-end can access databases, you are looking for something in that particular database, whose result can be "misson accomplished" or "404 not found".
You can use a shortcut in Django like get_object_or_404.
from django.shortcuts import get_object_or_404
...
question = get_object_or_404(Question,pk=question_id)
return render(request, 'question/details.html', {'question':question})
...