In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Django how to request HTML page view information, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
1. Write functional functions for querying data
The views.py file in the application directory is usually used to save functions or classes that respond to various requests.
From django.shortcuts import renderfrom .models import BlogArticles# Create your views here.def blog_title (request): # request is responsible for responding to received requests # query to get all instance data in all BlogArticles tables blogs = BlogArticles.objects.all () "" render () function is to render the data to a specified template "blog/titles.html" specifies which page to render to The page path is the templates directory under the application directory {"blogs": blogs} is the data passed to the page "" return render (request, "blog/titles.html", {"blogs": blogs}) 2. Create relevant html page files in the application directory
The file directory structure is as follows:
Write related page code
{% block title%} {% endblock%} {% block content%} {% endblock%} {% extends "base.html"%} {% block title%} blog title {% endblock%} {% block content%} my blog {% for blog in blogs%} {{blog.title}} {% endfor%} {% endblock%} 3. Configure URL
Write the urls.py file under the project directory
From django.contrib import adminfrom django.urls import path, includeurlpatterns = [path ('admin/', admin.site.urls), path ("blog/", include (' blog.urls')), # divert the blog/ request here to the urls.py of the blog application, that is,. / blog/urls.py file]
Write the urls.py file under the application directory
From django.urls import pathfrom. The first parameter of import views "" is empty, which means that the root is accessed, because the file is in the blog application, and the function that responds to the request "urlpatterns = [path (', views.blog_title),] is used for blog/views.blog_title."
Start project access: http://127.0.0.1:8000/blog/ to see the result
4. Pass parameters to url
Modify the titles.html file to add a link to the article title
{% for blog in blogs%} {{blog.title}} {% endfor%}
Write the corresponding function (. / blog/views.py) for the request
Def blog_article (request, article_id): # the request passes the article_id parameter article = BlogArticles.objects.get (id=article_id) pub = article.publish return render (request, "blog/content.html", {"article": article, "publish": pub})
Write a html page (templates/blog/content.html) that displays the contents of the file
{% extends "base.html"%} {% block title%} blog title {% endblock%} {% block content%} {{article.title}} {{publish}}
{{article.body}} {% endblock%}
Add the url (. / blog/urls.py) corresponding to the request
Urlpatterns = [path ('', views.blog_title), path ('/', views.blog_article),] URL configuration and query
When a user requests a URL through a browser, the Django will query the URLConf according to the request path and take the first qualified mapping as the query result.
For example, to access http://localhost:8000/blog/1, the request process is as follows:
Localhost:8000: the main domain name, which is not queried
/ blog/: first queries the project directory / urls.py and encounters a qualified URL mapping (path ('blog/', include (' blog.urls')),). According to the description in this mapping, query it in blog.urls (. / blog/urls.py)
/ 1: URL (path ('/', views.blog_article)) is configured in. / blog/urls.py, and the request path matches exactly, thus determining the view function views.blog_article that is finally accessed.
After reading the above, have you mastered the method of how to request HTML page view information in Django? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.