django를 이용하여 간단한 블로그 사이트를 만드는 예시

1. 페이지 구현
index.html
base.html
post.html
header.html
footer.html

<!-- index.html-->
{% extends 'base.html' %}
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title> </title>
</head>
<body>
<h1> </h1>
{% for post in posts %}
  <hr>
  <p style="font-family:   ">
  <a href="/post/{{ post.slug }}" rel="external nofollow" rel="external nofollow" >{{ post.title }}</a>
  </p>
{% endfor %}
<br>
{{ now }}
</body>
</html>
<div class="mainContext">
  <div class="rightContext">
    {% block title %} {% endblock %}
    {% block headmessage %}<h3 style="font:  ;"> </h3>{% endblock %}
    {% block content %}
    <ul>
      {% for post in posts %}
        <p>
          <li><a href="/post/{{ post.slug }}" rel="external nofollow" rel="external nofollow" >{{ post.title }}</a></li>
        </p>
      {% endfor %}
    </ul>
    {% endblock %}
</div>
</div>

<!-- base.html-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>{% block title %} {% endblock %}</title>
</head>
<body>
<div class="mainContext">
  <div class="leftContext">
    <h3 style="font:  ;"> </h3>
    <ul>
      <li><a href="/tag/?p= " rel="external nofollow" > </a></li>
      <li><a href="/tag/?p= " rel="external nofollow" > </a></li>
      <li><a href="/tag/?p= " rel="external nofollow" > </a></li>
    </ul>
  </div>
  <div class="rightContext">
    <div class="top1">
    {% include 'header.html' %}
  </div>
  <div class="mid2">
    {% block headmessage %} {% endblock %}
    {% block content %} {% endblock %}
  </div>
  <div class="bot3">
    <br/>
    {% include 'footer.html' %}
  </div>
  </div>
</div>
</body>
</html>

<!-- post.html-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>post</title>
</head>
<body>
<a href="http://localhost:8000/" rel="external nofollow" > </a><br/>
{{ post.body }}
</body>
</html>

<!-- footer.html-->
{% block footer %}
  {% if now %}
    <p style="font-family:  "> :{{ now }}</p>
  {% else %}
    <p style="font-family:  "> </p>
  {% endif %}
{% endblock %}
models.py 데이터 테이블의 디자인

from django.db import models
from django.utils import timezone
from tinymce.models import HTMLField
# Create your models here.
class Post(models.Model):
  title = models.CharField(max_length = 200,verbose_name=u' ')# 
  slug = models.CharField(max_length=200,verbose_name=u' ')# 
  body = models.TextField()# 
  tags = models.CharField(max_length=100, verbose_name=u' ')
  pub_date = models.DateTimeField(default = timezone.now)# 

  #pub_date  timezone.now   pytz 
  class Meta:
    db_table = ' '
    ordering = ['pub_date']# 
    def __str__(self):# , 
      return self.title
데이터 테이블의 이동은 cmd에서 실행됩니다

python manage.py makemigrations
python manage.py migrate
views.py 방법의 실현

#   
def homepage(request):
  posts = Post.objects.all().order_by('-pub_date')
  return render(request, 'index.html', locals())
  now = datetime.now()
  # 
def show_detail(request,slug):
  try:
    post = Post.objects.get(slug = slug)
    if post != None:
      return render(request,'post.html',locals())
  except:
    return redirect('/')# 
# views 
def search_tag(request): #tag URL 
  tag = request.GET.get('p')
  print(tag)
  try:
    posts = Post.objects.filter(tags=tag)# filter
    if posts != None:# posts, index.html 
      return render(request,'index.html',locals())
  except:
    print(' ')
url.py URL에 경로 등록

from django.conf.urls import url, include
from django.contrib import admin
from django.urls import path
from myblogs import views
#import tinymce
urlpatterns = [
  path('', views.homepage),# 
  path('admin/', admin.site.urls),# 
  path('post/<slug:slug>/',views.show_detail),# #  ,   
  url(r'^tag/$', views.search_tag)# url    
  #url(r'^tinymce/', include('tinymce.urls')), #  
]
인터페이스에 css나 그림을 추가합니다.
설정 설정

STATIC_URL = '/static/'
STATICFILES_DIRS = [
  os.path.join(BASE_DIR, 'static'),
]
인터페이스에 도입

1. 
{% load staticfiles %}
<title>{% block title %} {% endblock %}</title>
2. 
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'index.css' %}" rel="external nofollow" >
이상은 바로django를 이용하여 간단한 블로그 사이트를 만드는 예시의 상세한 내용입니다. 더 많은 django 사이트 창설에 관한 자료는 저희 다른 관련 글을 주목해 주십시오!

좋은 웹페이지 즐겨찾기