· 8 years ago · Jan 29, 2018, 03:56 AM
1"""
2Django settings for mysite project.
3
4Generated by 'django-admin startproject' using Django 1.8.6.
5
6For more information on this file, see
7https://docs.djangoproject.com/en/1.8/topics/settings/
8
9For the full list of settings and their values, see
10https://docs.djangoproject.com/en/1.8/ref/settings/
11"""
12
13# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
14import os
15
16BASE_DIR = os.path.dirname(os.path.dirname(__file__))
17
18
19# Quick-start development settings - unsuitable for production
20# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
21
22# SECURITY WARNING: keep the secret key used in production secret!
23SECRET_KEY = 't$6p8^=z+%8_zm+qb%s8&!!sh@j%)lg4byd@nc8(s5#ozoz&-l'
24
25# SECURITY WARNING: don't run with debug turned on in production!
26DEBUG = True
27
28ALLOWED_HOSTS = []
29
30
31# Application definition
32
33INSTALLED_APPS = (
34 'django.contrib.admin',
35 'django.contrib.auth',
36 'django.contrib.contenttypes',
37 'django.contrib.sessions',
38 'django.contrib.messages',
39 'django.contrib.staticfiles',
40 'blog',
41 'taggit',
42)
43
44
45MIDDLEWARE_CLASSES = (
46 'django.contrib.sessions.middleware.SessionMiddleware',
47 'django.middleware.common.CommonMiddleware',
48 'django.middleware.csrf.CsrfViewMiddleware',
49 'django.contrib.auth.middleware.AuthenticationMiddleware',
50 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
51 'django.contrib.messages.middleware.MessageMiddleware',
52 'django.middleware.clickjacking.XFrameOptionsMiddleware',
53 'django.middleware.security.SecurityMiddleware',
54)
55
56ROOT_URLCONF = 'mysite.urls'
57
58
59TEMPLATES = [
60 {
61 'BACKEND': 'django.template.backends.django.DjangoTemplates',
62 'DIRS': [],
63 'APP_DIRS': True,
64 'OPTIONS': {
65 'context_processors': [
66 'django.template.context_processors.debug',
67 'django.template.context_processors.request',
68 'django.contrib.auth.context_processors.auth',
69 'django.contrib.messages.context_processors.messages',
70 ],
71 },
72 },
73]
74
75WSGI_APPLICATION = 'mysite.wsgi.application'
76
77
78# Database
79# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
80
81DATABASES = {
82 'default': {
83 'ENGINE': 'django.db.backends.sqlite3',
84 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
85 }
86}
87
88# Internationalization
89# https://docs.djangoproject.com/en/1.8/topics/i18n/
90
91LANGUAGE_CODE = 'en-us'
92
93TIME_ZONE = 'UTC'
94
95USE_I18N = True
96
97USE_L10N = True
98
99USE_TZ = True
100
101
102# Static files (CSS, JavaScript, Images)
103# https://docs.djangoproject.com/en/1.8/howto/static-files/
104
105STATIC_URL = '/static/'
106
107from django.conf.urls import include, url
108from django.contrib import admin
109
110
111urlpatterns = [
112 url(r'^admin/', include(admin.site.urls)),
113 url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')),
114]
115
116from django.conf.urls import url
117from . import views
118
119
120 urlpatterns = [
121 # post views
122 url(r'^$', views.post_list, name='post_list'),
123 #url(r'^$', views.PostListView.as_view(), name='post_list'),
124 url(r'^(?P<year>d{4})/(?P<month>d{2})/(?P<day>d{2})/(?P<post>[-w]+)/$',
125 views.post_detail,
126 name='post_detail'),
127]
128
129from django.shortcuts import render, get_object_or_404
130from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
131from django.views.generic import ListView
132from .models import Post
133
134
135def post_list(request, category=None):
136 object_list = Post.published.all()
137 paginator = Paginator(object_list, 3) # 3 posts in each page
138 page = request.GET.get('page')
139 try:
140 posts = paginator.page(page)
141 except PageNotAnInteger:
142 # If page is not an integer deliver the first page
143 posts = paginator.page(1)
144 except EmptyPage:
145 # If page is out of range deliver last page of results
146 posts = paginator.page(paginator.num_pages)
147 return render(request, 'blog/post/list.html', {'page': page,
148 'posts': posts})
149
150
151class PostListView(ListView):
152 queryset = Post.published.all()
153 context_object_name = 'posts'
154 paginate_by = 3
155 template_name = 'blog/post/list.html'
156
157
158def post_detail(request, year, month, day, post):
159 post = get_object_or_404(Post, slug=post,
160 status='published',
161 publish__year=year,
162 publish__month=month,
163 publish__day=day)
164 return render(request, 'blog/post/detail.html', {'post': post})