django1.6 博客的快速搭建second blog v2
2014-11-11 00:22
766 查看
一:效果总览
这是在前一篇博文的基础上改进的博客,还没有美化页面,暂且学习中就这样吧,以后版本改进中首先看下具体效果:
127.0.0.1/articles/2014
127.0.0.1/news/articles/2014
上面是两种形式的url mapping,都可以访问,结果一样
只查看2014年的博客;
博客的列表
博客文章的详细页面:
二:博客目录结构
三:几个重要的文件
news目录
views.py --视图处理函数
[root@localhost news]# pwd /root/Desktop/data/download/django/mysite16_5_demo2/news [root@localhost news]# more views.py from django.shortcuts import render from django.shortcuts import render_to_response from news.models import Article # Create your views here. def year_archive(request, year): a_list = Article.objects.filter(pub_date__year=year) return render_to_response('year_archive.html', {'year': year, 'article_list': a_list}) def list_article(request): lit_list=Article.objects.all() return render_to_response('list_article.html',{'literary' : lit_list}) def detail_article(request,serial): show_detail=Article.objects.filter(id=serial) return render_to_response('detail_article.html',{'details' : show_detail,'id' : serial}) [root@localhost news]#
models.py --模型
[root@localhost news]# pwd /root/Desktop/data/download/django/mysite16_5_demo2/news [root@localhost news]# more models.py from django.db import models #from django.contrib import admin # Create your models here. class Reporter(models.Model): full_name = models.CharField(max_length=70) def __unicode__(self): return self.full_name class Article(models.Model): pub_date = models.DateField() headline = models.CharField(max_length=200) content = models.TextField() reporter = models.ForeignKey(Reporter) def __unicode__(self): return self.headline #admin.site.register(Reporter,Article) [root@localhost news]#
urls.py -- urlconfig
[root@localhost news]# pwd /root/Desktop/data/download/django/mysite16_5_demo2/news
[root@localhost news]# more urls.py #from django.conf.urls import patterns from django.conf.urls import * from news.views import year_archive from news.views import * urlpatterns = patterns('', #url(r'^articles/(\d{4})/$', year_archive), (r'^articles/(\d{4})/$', year_archive), (r'^articles/list/$', list_article), (r'^articles/detail/(\d+)/$', detail_article), (r'^articles/(\d{4})/(\d{2})/$', 'month_archive'), (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'), ) [root@localhost news]#目前只用到了前面三个
(r'^articles/(\d{4})/$', year_archive)匹配到这样的url:127.0.0.1/news/articles/2014
admin.py ----后台模块注册
[root@localhost news]# pwd /root/Desktop/data/download/django/mysite16_5_demo/news [root@localhost news]# more admin.py from django.contrib import admin import models # Register your models here. admin.site.register(models.Article) [root@localhost news]#
news/templates目录
[root@localhost news]# cd templates/ [root@localhost templates]# ll total 16 -rw-r--r--. 1 root root 221 Nov 10 17:23 base.html -rw-r--r--. 1 root root 311 Nov 10 18:02 detail_article.html -rw-r--r--. 1 root root 370 Nov 10 18:24 list_article.html -rw-r--r--. 1 root root 449 Nov 10 18:16 year_archive.html [root@localhost templates]# more base.html {% load staticfiles %} <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <img src="{% static "images/sitelogo.png" %}" alt="Logo" /> {% block content %}{% endblock %} </body> </html> [root@localhost templates]# more detail_article.html {% extends "base.html" %} {% block title %}Articles for {{ id }}{% endblock %} {% block content %} <h1>this is my blog article detail</h1> {% for detail in details %} <h3>{{ detail.headline }}</h3> <small>Published {{ literary.pub_date|date:"F j, Y" }}</small> {{ detail.content }} {% endfor %} {% endblock %} [root@localhost templates]# more list_article.html {% extends "base.html" %} {% block title %}Articles for {{ id }}{% endblock %} {% block content %} <h1>here is my blogs first version2</h1> {% for literary_list in literary %} <h3><a href="/news/articles/detail/{{ literary_list.id }}">{{ literary_list.headline }}</a></h3> <small>Published {{ literary_list.pub_date|date:"F j, Y" }}</small> {% endfor %} {% endblock %} [root@localhost templates]# more year_archive.html {% extends "base.html" %} {% block title %}Articles for {{ year }}{% endblock %} {% block content %} <h1>Articles for {{ year }}</h1> {% for article in article_list %} <h1>{{ article.headline }}</h1> <h3>By {{ article.reporter.full_name }}</h3> content-------------->>>>>>> <p>{{ article.content }}</p> <p>id is: {{ article.id }}</p> <small>Published {{ article.pub_date|date:"F j, Y" }}</small> {% endfor %} {% endblock %} [root@localhost templates]#
templates目录可以在这里设置
[root@localhost mysite16_5_demo]# pwd /root/Desktop/data/download/django/mysite16_5_demo2/mysite16_5_demo
TEMPLATE_DIRS = ( '/root/Desktop/data/download/django/mysite16_5_demo2/news/templates', )如果没设置该目录的话,默认好像会在当前app下的templates目录下寻找,顺序为先在settings.py中设置的目录中寻找,然后才在默认目录
static目录
该目录下存放的是静态文件,下面详细介绍静态文件的使用[root@localhost images]# pwd /root/Desktop/data/download/django/mysite16_5_demo2/news/static/images [root@localhost images]# ll total 16 -rw-r--r--. 1 root root 14197 Nov 10 17:23 sitelogo.png [root@localhost images]#
在setting.py中设置静态文件目录
STATIC_URL = '/static/'
在模版中使用静态文件
<body> <img src="{% static "images/sitelogo.png" %}" alt="Logo" /> {% block content %}{% endblock %} </body>
疑问;这里的STATIC_URL的绝对路径设置的很有疑惑,暂且先这样弄吧
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
setting.py文件 -- 项目设置
[root@localhost mysite16_5_demo]# pwd /root/Desktop/data/download/django/mysite16_5_demo2/mysite16_5_demo
[root@localhost mysite16_5_demo]# more settings.py
"""
Django settings for mysite16_5_demo project.
For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xaa+uyd$dx&$e50m4x=-7&a02gd@)2m!o%kf=ldnf1u4$d$tu^'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
TEMPLATE_DIRS = ( '/root/Desktop/data/download/django/mysite16_5_demo2/news/templates', )
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'news',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite16_5_demo.urls'
WSGI_APPLICATION = 'mysite16_5_demo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
[root@localhost mysite16_5_demo]#
系统级的urls文件
[root@localhost mysite16_5_demo]# pwd /root/Desktop/data/download/django/mysite16_5_demo2/mysite16_5_demo
[root@localhost mysite16_5_demo]# more urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite16_5_demo.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
url(r'^news/', include('news.urls')),
url(r'^admin/', include(admin.site.urls)),
)
[root@localhost mysite16_5_demo]#
这里的URL能匹配到
127.0.0.1/articles/2014
这就为什么可以同时匹配到
127.0.0.1/articles/2014
127.0.0.1/news/articles/2014
总结:
在这个过程中遇到过一些问题,皆因自己的不细心导致,没什么需要特别注意的
在python shell中后台操作的过程中,需要先执行,具体解决过程如下:
[root@localhost mysite16_5_demo]# python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from news.models import Reporter, Article
File "<stdin>", line 1
from news.models import Reporter, Article
^
IndentationError: unexpected indent
>>> from news.models import Reporter, Article
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "news/models.py", line 1, in <module>
from django.db import models
File "/usr/lib/python2.6/site-packages/django/db/models/__init__.py", line 5, in <module>
from django.db.models.query import Q
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 17, in <module>
from django.db.models.deletion import Collector
File "/usr/lib/python2.6/site-packages/django/db/models/deletion.py", line 4, in <module>
from django.db.models import signals, sql
File "/usr/lib/python2.6/site-packages/django/db/models/sql/__init__.py", line 4, in <module>
from django.db.models.sql.subqueries import *
File "/usr/lib/python2.6/site-packages/django/db/models/sql/subqueries.py", line 12, in <module>
from django.db.models.sql.query import Query
File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 22, in <module>
from django.db.models.sql import aggregates as base_aggregates_module
File "/usr/lib/python2.6/site-packages/django/db/models/sql/aggregates.py", line 9, in <module>
ordinal_aggregate_field = IntegerField()
File "/usr/lib/python2.6/site-packages/django/db/models/fields/__init__.py", line 116, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
>>> from django.conf import settings
>>> settings.configure()
>>> from news.models import Reporter, Article
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "news/models.py", line 1, in <module>
from django.db import models
File "/usr/lib/python2.6/site-packages/django/db/models/__init__.py", line 5, in <module>
from django.db.models.query import Q
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 17, in <module>
from django.db.models.deletion import Collector
File "/usr/lib/python2.6/site-packages/django/db/models/deletion.py", line 4, in <module>
from django.db.models import signals, sql
ImportError: cannot import name signals
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite16_5_demo.settings")
相关文章推荐
- django1.6 博客的快速搭建first blog
- Django+python+eclipse 快速搭建博客blog .
- Django+python+eclipse 快速搭建博客blog
- Django1.11.1+python3.5+Pycharm 快速搭建博客blog
- Django+python+eclipse 快速搭建博客blog
- django快速搭建简单blog
- Django快速搭建Blog
- Django:快速搭建简单的Blog
- django 快速搭建blog
- 通过Z-BlogPHP程序快速在论坛里搭建一个博客页面
- 使用 django-blog-zinnia 搭建个人博客
- python django 快速搭建blog
- django 快速搭建blog
- django 快速搭建blog
- 通过Z-BlogPHP程序快速在论坛里搭建一个博客页面
- Django:快速搭建自己打第一个的Blog
- 使用GitHub pages + Hexo 快速搭建个性化博客(一)-部署Hexo默认blog模板至远程GitHub
- Django:快速搭建简单的Blog
- django 快速搭建blog
- Django快速学习搭建blog项目