zoukankan      html  css  js  c++  java
  • django入门与实践

    第一种方法:

    在myblog/urls.py模块中:

    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('blog1/', include(('blog1.urls', 'a'), namespace='blogg')),  #'a'可以为任意字符,但不能为空
    ]
    myblog/urls.py

    在blog/urls.py模块中:

    from django.urls import path
    from . import views
    urlpatterns = [
        path('', views.index),  # 这是路由模式
        path('article/<int:article_id>', views.article_page, name='article_detai'),  # path中的组名必须和参数名一致
    ]
    blog/urls.py

    在blog/templates/index.html模板中:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1>
            <a href="">添加新文章</a>
        </h1>
        {% for article in articles %}
            <h2>
                <a href="{% url 'blogg:article_detai' article.id %}">{{ article.title }}</a>
            </h2>
        {% endfor %}
    </body>
    </html>
    blog/templates/index.html

    在blog/view.py模块中:

    from django.shortcuts import render
    from blog1 import models
    
    # 方法:index()
    # 参数:request:
    # 功能:将Article表中的数据取出,在页面上显示所有文章
    def index(request):
        
        articles = models.Article.objects.all()
        return render(request, 'blog1/index.html', {'articles': articles})
    
    
    # 方法:article_page()
    # 参数:request:
    #      article_id:文章id,唯一标识符
    # 功能:通过接收article_id,显示对应文章的详细内容
    def article_page(request, article_id):
    
        article = models.Article.objects.get(pk=article_id)
        return render(request, 'blog1/article_detail.html', {'article': article})
    blog/view.py

    第二种方法:

  • 相关阅读:
    数据结构课后
    idea 使用java 链接sqlservice 2008
    超链接 a href 提交表单通过post方式
    课程主页之课程接口
    课程主页之课程表数据
    课程表分析
    课程前端简单页面
    前台的登录注册
    ORM常用字段及参数与查询 -刘
    Celery配置与使用
  • 原文地址:https://www.cnblogs.com/chenjianfeng/p/10579815.html
Copyright © 2011-2022 走看看