M
未更改模型
from django.db import models
# Create your models here.
class People(models.Model):
name = models.CharField(null=True, blank=True,max_length=200)
job = models.CharField(null=True, blank=True, max_length=200)
def __str__(self):
return self.name
class Article(models.Model):
headline = models.CharField(null=True, blank=True,max_length=500)
content = models.TextField(null=True, blank=True)
TAG_CHOICES = (
('tech', 'Tech'),
('life','Life'),
)
tag = models.CharField(null=True, blank=True, max_length=5, choices=TAG_CHOICES)
def __str__(self):
return self.headline
class Comment(models.Model):
name = models.CharField(null=True, blank=True, max_length=50)
comment = models.TextField()
def __str__(self):
return self.comment
U
from django.conf.urls import url
from django.contrib import admin
from firstapp.views import index,detail
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index', index, name='index'),#url(‘url地址’,函数名,查找方便名)
url(r'^detail/(?P<page_num>\d+)$', detail, name='detail'),
#新增
#url(r'^detail/(?P<page_num>\d+)$', detail, name='detail'),
#(?P<page_num>\d+)------把\d+命名为?P<page_num>【page_num为数据库默认文章id】
]
V
from django.shortcuts import render, HttpResponse, redirect
from firstapp.models import Article, Comment
from django.template import Context, Template
from firstapp.form import CommentForm
# Create your views here.
def index(request):
print(request)
print('==='*30)
print(dir(request))
print('==='*30)
print(type(request))
queryset = request.GET.get('tag')
if queryset:
article_list = Aritcle.objects.filter(tag=queryset)
else:
article_list = Article.objects.all()
context = {}
context['article_list'] = article_list
index_page = render(request, 'first_web_2.html', context)
return index_page
def detail(request, page_num):#新增参数page_num
if request.method == 'GET':
form = CommentForm
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
comment = form.cleaned_data['comment']
c = Comment(name=name, comment=comment)
c.save()
return redirect(to='detail')
context = {}
comment_list = Comment.objects.all()
article = Article.objects.get(id=page_num)#从数据库中找到id=page_num的文章
context['article'] = article#装入context上下文中
context['comment_list'] = comment_list
context['form'] = form
return render(request, 'article_detail.html', context)
T
article_detail.html
在文章详情页添加文章标题变量{{ article.headline }}和文章内容变量{{ article.content }}
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=0swald|Raleway" rel="stylesheet">
<style type="text/css">
.ui.segment.container{
width:700px;
}
p {
font-family:'Raleway', sans-serif;
font-size:18px;
}
body {
background: url({% static 'images/star_banner.jpg' %});
background-size:cover;
background-repeat:no-repeat;
background-attachment:fixed
}
</style>
</head>
<body>
<div class="ui image">
<img src="" alt="" />
</div>
<div class="ui segment padded container">
<h1 class="ui header" style="font-family:'Oswald',sans-serif;font-size: 40px">
{{ article.headline }}
</h1>
<p>
{{ article.content }}
</p>
</div>
<div class="ui segment container" style="width:700px;">
<h3 class="ui header" style="font-family: 'Oswald', sans-serif;">Comments</h3><!--标题Comment-->
<div class="ui comments"><!--评论-->
{% for comment in comment_list %}
<div class="comment">
<div class="avatar">
<img src="http://semantic-ui.com/images/avatar/small/matt.jpg" alt="" />
</div>
<div class="content">
<a href="#" class="author">{{ comment.name }}</a>
<div class="metadata">
<div class="date">2 days ago</div>
</div>
<p class="text" style="font-family:'Raleway', sans-serif;">
{{ comment.comment }}
</p>
</div>
</div>
{% endfor %}
</div>
<div class="ui divider"></div><!--分割线评论-->
<form class="ui error tiny form" method="post">
{% if form.errors %}
<div class="ui error message">
{{ form.errors }}
</div>
{% for field in form %}
<div class="{{ field.errors|yesno:'error, '}} field"><!--#依次判断表格中的field是否有error-->
{{ field.label }}
{{ field }}
</div>
{% endfor %}
{% else %}
{% for field in form %}
<div class="field">
{{ field.label }}
{{ field }}
</div>
{% endfor %}
{% endif %}
{% csrf_token %}<!--Django防跨站"{%csrf_token%}"标签-->
<button type="submit" class="ui blue button">Click</button>
</form>
</div>
</body>
</html>
first_web_2.html
在列表页面添加每个文章的链接href=”{% url ‘detail’ article.id %}”
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>first web</title>
<link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Oswald|Raleway" rel="stylesheet">
<style type="text/css">
h1 {
font-family:'Oswald', sans-serif!important;
font-size:40px;
}
body {
font-family: 'Raleway', sans-serif;
}
p {
font-family: 'Raleway', sans-serif;
font-size:18px;
}
.ui.vertical.segment.masthead {
height: 300px;
background-image: url({% static 'images/star_banner.jpg' %});
background-size: cover;
background-position: 100% 80%;
}
.ui.container.segment {
width: 800px;
}
.ui.center.aligned.header.blogslogon {
margin-top: 40px;
}
.ui.center.aligned.header.blogslogon p {
margin-top: 10px;
color: white;
font-size: 10px;
}
.ui.container.nav {
width: 500px;
}
</style>
</head>
<body>
<div class="ui inverted vertical segment masthead">
<h1 class="ui center aligned header blogslogon" style="font-size:50px;font-family: 'Raleway', sans-serif!important;">
Bloger
<p class="ui sub header">
everyone has a story to tell
</p>
</h1>
</div>
<div class="ui container nav">
<div class="ui borderless text three item menu ">
<div class="ui simple dropdown item">
Categories
<i class="dropdown icon"></i>
<div class="menu">
<a class="item" href="?tag=life">life</a>
<a class="item" href="?tag=tech">tech</a>
</div>
</div>
<a class="item">
Popular
</a>
<a class="item">
About
</a>
</div>
</div>
<div class="ui divider"></div>
<div class="ui vertical segment">
{% for article in article_list %}
<div class="ui container vertical segment">
<a href="{% url 'detail' article.id %}">
<h1 class="ui header">
{{ article.headline }}
</h1>
</a>
<i class="icon grey small unhide">10,000</i>
<p>
{{ article.content|truncatewords:100 }}
<a href="{% url 'detail' article.id %}">
<i class="angle tiny double grey right icon">READMORE</i>
</a>
</p>
<div class="ui mini tag label">
{{ article.tag }}
</div>
</div>
{% endfor %}
</div>
<div class="ui inverted vertical very padded segment">
Mugglecoding®
</div>
</body>
</html>
运行顺序是
T点击页面的href="{% url 'detail' article.id %}"
【http://127.0.0.1:8000/detail/2】
U通过url索引找到url(r'^detail/(?P<page_num>\d+)$', detail, name='detail'),
把detail后面的\d+数字传入并命名为page_num变量
V 传入page_num【即前端页面的article.id,在U层被命名为page_num】找到指定文章
def detail(request, page_num):
article = Article.objects.get(id=page_num)
context['article'] = article
return render(request, 'article_detail.html', context)