{{ post.title }}
{{ post.content }}
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog',
'USER': 'name',
'PASSWORD':'pass',
'HOST':'localhost',
'PORT':'3306',
}
}
# encoding:utf8
from django.db import models
from django.contrib import admin
class Article(models.Model):
title = models.CharField(max_length=100) #
category = models.TextField(max_length=50, blank=True) #
date_time = models.DateTimeField(auto_now_add=True) #
content = models.TextField(blank=True, null = True) #
def __unicode__(self):
return self.title
class Meta: #
ordering = ['-date_time']
# Register model
admin.site.register(Article)
python manage.py migrate
python manage.py makemigrations
python manage.py migrate
'DIRS': [BASE_DIR+"/templates",],
Vincent's Blog
{% block content %}
{% endblock %}
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
{{ post.title }}
{{ post.content }}
{% endfor %}
{% endblock %}
from blog.models import Article
from datetime import datetime
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def home(request):
post_list = Article.objects.all()
return render(request, 'home.html', {'post_list':post_list})
def detail(request, my_args):
post = Article.objects.all()[int(my_args)]
str = ("title = %s, category = %s, date_time = %s, content = %s"
% (post.title, post.category, post.date_time, post.content))
return HttpResponse(str)
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',home),
]
python manage.py createsuperuser