1、打开article文件夹中的views.py,输入一下内容
from django.shortcuts import render
from django.http import HttpResponse
def article_detail(request,article_id):
return HttpResponse(“文章id:%s”% article_id)
2、打开mysite文件夹中的urls.py,输入以下内容:
from django.contrib import admin
from django.urls import path
from . import views
from article.views import article_detail###新增
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(’’,views.index),
path(‘article/int:article_id’,article_detail,name=‘article_detail’)####新增
]
3、然后在article 文件夹中打开views.py,更改后的内容为:
from django.shortcuts import render
from django.http import HttpResponse,Http404
from .models import Article
def article_detail(request,article_id):
try:
article = Article.objects.get(id=article_id)
except Article.DoesNotExist:
raise Http404(“对不起,您访问的网页不存在!”)
return HttpResponse(“文章标题:%s 文章内容:%s”% (article.title,article.content))
为了更改前端显示模式,需要在article 文件夹中新建templates文件夹