想要为django中的每个博客帖子创建单独的页面?但是不知道如何进行?

时间:2019-06-18 08:29:34

标签: python django

我正在尝试使用django创建一个博客应用程序。我在应用程序中有一个主页,其中将显示所有博客文章。如果有人单击博客文章,则应将其显示在单独的页面上,但不知道如何继续?

我知道我无法为每个帖子创建单独的html页面。我创建了一个用于显示每个帖子的通用布局html页面,但是不知道如何向其中添加内容。

 <!---------layout.html---->
  {% load static %}
 <html>
 <head>
    <title>Self Learn</title>
    <link rel="stylesheet" type="text/css" href="{% static ' 
                                                 styles/main.css' %}" />
 </head>
 <body>    
    <div class="header">
        <h1 class="logo">Blog</h1> 
        <ul class="menu">
            <a href="/">Home</a>
            <a href="#">About Us</a>
            {% if user.is_authenticated %}
            <a>Hi,{{user.username}}</a>
            <a href="logout">Logout</a>
            {% else %}
            <a href="login">Login</a>
            <a href='register'>Register</a>
            {% endif %}
        </ul>
     </div>
     <a href="newpost">
        <img src="{% static 'images\12.PNG' %}" class="logo1"/>
     </a>
     <!----Post------>
     {% for blog in blog %}
     <div class="list">
        <div class="con">
            <h3 style="color:DodgerBlue;">{{blog.author}}</h3>
            <h6 style="font-family: montserrat,sans-serif;"> {{blog.date}} 
                                                                    </h6>
        </div>
        <div class="line"></div>
        <div class="con">
            <a href='indpost'><h1><b>{{blog.title}}</b></h1></a>
        </div>
        <div class="con">
            <p>{{blog.desc}}</p>
        </div>
     </div>
     {% endfor %}
     {% block content %}
     {% endblock %}
 </body>
 </html>
    from django.shortcuts import render,redirect
    from blog.models import Post
    from django.contrib.auth.models import User,auth
    from django.contrib import messages

    # Create your views here.
    def homepage(request):
        blogs= Post.objects.all()
        return render(request,'layout.html',{'blog':blogs})

    def register(request):
        if request.method == "POST":
            email=request.POST['email']
            User_name=request.POST['User_name']
            Password1=request.POST['Password1']
            Password2=request.POST['Password2']
            if Password1 == Password2:
                if User.objects.filter(username=User_name).exists():
                    messages.info(request,'Username Taken')
                    return redirect('register')
                elif User.objects.filter(email=email).exists():
                    messages.info(request,'Email Taken')
                    return redirect('register')
                else:
                    user = 
 User.objects.create_user(username=User_name,password=Password1,email=email)
                    user.save();
                    return redirect('/')
            else:
                messages.info(request,'Password Not Matching')
        else:
            return render(request,'register.html')

    def login(request):
        if request.method == "POST":
            User_name=request.POST['User_name']
            Password=request.POST['Password']
            user = auth.authenticate(username=User_name,password=Password)
            if user is not None:
                auth.login(request,user)
                return redirect("/")
            else:
                messages.info(request,"invalid")
                redirect('login')
        else: 
            return render(request,'Login.html')

    def logout(request):
        auth.logout(request)
        return redirect('login')

    def newpost(request):
        if request.user.is_authenticated:
            if request.method == "POST":
                title = request.POST['title']
                desc=request.POST['desc']
                insert = 
                Post(title=title,desc=desc,author_id=request.user.username)
                insert.save();
                return redirect('/')
            else:
                return render(request,'Newpost.html')
        else:
            return redirect('login')

    def indpost(request):
        return render(request,'indpost.html')

  #urls.py
    from django.urls import path,include
    from . import views

    urlpatterns=[
            path('',views.homepage),
            path('register',views.register,name='register'),
            path('login',views.login,name='login'),
            path('logout',views.logout,name='logout'),
            path('newpost',views.newpost,name="newpost"),
            path('indpost',views.indpost,name="indpost"),
    ]

2 个答案:

答案 0 :(得分:0)

您可以添加以下内容,以获取带有slug和models.py中其他选项的特定文章:

 def get_absolute_url(self):
     return reverse('singlepage',
            args=[self.slug])

其中单页是您用于处理请求的视图的名称,

您可以添加视图以将请求的url作为上下文传递,并以以下方式呈现通用模板:

from django.shortcuts import render, get_object_or_404, reverse    
def singlepage (request, slug) :
    article = get_object_or_404(<model_name>, slug=post, status='published')
    return render(request,'news/singlepage.html',{'article': article})

您也可以在urls.py中添加对url的引用,例如:

path('<slug:post>/',views.singlepage,name='singlepage')

然后在您的html中,您可以访问文章上下文并将其标题称为{{ article.title }},依此类推

希望这会有所帮助

您可以在以下链接上引用博客应用程序:https://github.com/PacktPublishing/Django-2-by-Example/tree/master/Chapter01/mysite

答案 1 :(得分:0)

我认为您所看到的称为“详细信息视图”,您可以通过多种方法来实现它。我建议使用Django的详细类视图。

Here is the link to the official docs

如果您想使用基于函数的视图,还可以查看我的blog I have created