Я пытаюсь создать приложение для блога с 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"),
]