6 |
7 | {{ post.published_date }}
8 |
9 |
10 |
{{ post.text|linebreaks }}
11 |
12 | {% endfor %}
13 | {% endblock content %}
14 |
--------------------------------------------------------------------------------
/blogq/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 | from django.utils import timezone
3 |
4 |
5 | class Post(models.Model):
6 | author = models.ForeignKey('auth.User')
7 | title = models.CharField(max_length=200)
8 | text = models.TextField()
9 | created_date = models.DateTimeField(
10 | default=timezone.now)
11 | published_date = models.DateTimeField(
12 | blank=True, null=True)
13 |
14 | def publish(self):
15 | self.published_date = timezone.now()
16 | self.save()
17 |
18 | def __str__(self):
19 | return self.title
20 |
--------------------------------------------------------------------------------
/blogq/templates/blogq/post_detail.html:
--------------------------------------------------------------------------------
1 | {% extends 'blogq/base.html' %}
2 |
3 | {% block content %}
4 |
5 | {% if post.published_date %}
6 |
7 | {{ post.published_date }}
8 |
9 | {% endif %}
10 | {% if user.is_authenticated %}
11 |
12 | {% endif %}
13 |
{{ post.title }}
14 |
{{ post.text|linebreaks }}
15 |
16 | {% endblock %}
17 |
--------------------------------------------------------------------------------
/blogq/static/css/blogq.css:
--------------------------------------------------------------------------------
1 | h1 a {
2 | color: #CD38FF;
3 | }
4 | body {
5 | padding-left: 15px;
6 | font-family: 'Lobster';
7 | }
8 | .page-header {
9 | background-color: #ff9400;
10 | margin-top: 0;
11 | padding: 20px 20px 20px 40px;
12 | }
13 |
14 | .page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active {
15 | color: #ffffff;
16 | font-size: 36pt;
17 | text-decoration: none;
18 | }
19 |
20 | .content {
21 | margin-left: 40px;
22 | }
23 |
24 | h1, h2, h3, h4 {
25 | font-family: 'Lobster', cursive;
26 | }
27 |
28 | .date {
29 | float: right;
30 | color: #828282;
31 | }
32 |
33 | .save {
34 | float: right;
35 | }
36 |
37 | .post-form textarea, .post-form input {
38 | width: 100%;
39 | }
40 |
41 | .top-menu, .top-menu:hover, .top-menu:visited {
42 | color: #ffffff;
43 | float: right;
44 | font-size: 26pt;
45 | margin-right: 20px;
46 | }
47 |
48 | .post {
49 | margin-bottom: 70px;
50 | }
51 |
52 | .post h1 a, .post h1 a:visited {
53 | color: #000000;
54 | }
55 |
--------------------------------------------------------------------------------
/blogq/templates/blogq/base.html:
--------------------------------------------------------------------------------
1 | {% load staticfiles %}
2 |
3 |
4 |
19 |
20 |
21 | {% block content %}
22 | {% endblock %}
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/blogq/migrations/0001_initial.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # Generated by Django 1.9 on 2016-06-06 13:49
3 | from __future__ import unicode_literals
4 |
5 | from django.conf import settings
6 | from django.db import migrations, models
7 | import django.db.models.deletion
8 | import django.utils.timezone
9 |
10 |
11 | class Migration(migrations.Migration):
12 |
13 | initial = True
14 |
15 | dependencies = [
16 | migrations.swappable_dependency(settings.AUTH_USER_MODEL),
17 | ]
18 |
19 | operations = [
20 | migrations.CreateModel(
21 | name='Post',
22 | fields=[
23 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
24 | ('title', models.CharField(max_length=200)),
25 | ('text', models.TextField()),
26 | ('created_date', models.DateTimeField(default=django.utils.timezone.now)),
27 | ('published_date', models.DateTimeField(blank=True, null=True)),
28 | ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
29 | ],
30 | ),
31 | ]
32 |
--------------------------------------------------------------------------------
/blogq/views.py:
--------------------------------------------------------------------------------
1 | from django.shortcuts import render
2 | from django.utils import timezone
3 | from .models import Post
4 | from django.shortcuts import render, get_object_or_404
5 | from .forms import PostForm
6 | from django.shortcuts import redirect
7 |
8 | def post_list(request):
9 | posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
10 | return render(request, 'blogq/post_list.html', {'posts': posts})
11 | def post_detail(request, pk):
12 | Post.objects.get(pk=pk)
13 | def post_detail(request, pk):
14 | post = get_object_or_404(Post, pk=pk)
15 | return render(request, 'blogq/post_detail.html', {'post': post})
16 | def post_new(request):
17 | if request.method == "POST":
18 | form = PostForm(request.POST)
19 | if form.is_valid():
20 | post = form.save(commit=False)
21 | post.author = request.user
22 | post.published_date = timezone.now()
23 | post.save()
24 | return redirect('post_detail', pk=post.pk)
25 | else:
26 | form = PostForm()
27 | return render(request, 'blogq/post_edit.html', {'form': form})
28 | def post_edit(request, pk):
29 | post = get_object_or_404(Post, pk=pk)
30 | if request.method == "POST":
31 | form = PostForm(request.POST, instance=post)
32 | if form.is_valid():
33 | post = form.save(commit=False)
34 | post.author = request.user
35 | post.published_date = timezone.now()
36 | post.save()
37 | return redirect('post_detail', pk=post.pk)
38 | else:
39 | form = PostForm(instance=post)
40 | return render(request, 'blogq/post_edit.html', {'form': form})
41 |
--------------------------------------------------------------------------------
/mysite/settings.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for mysite project.
3 |
4 | Generated by 'django-admin startproject' using Django 1.9.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/1.9/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/1.9/ref/settings/
11 | """
12 |
13 | import os
14 |
15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 |
18 |
19 | # Quick-start development settings - unsuitable for production
20 | # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
21 |
22 | # SECURITY WARNING: keep the secret key used in production secret!
23 | SECRET_KEY = '8v)!244-ovau=rbjl^4a*m^bsv9$2vw)%k0kj+stm&4p4b#75w'
24 |
25 | # SECURITY WARNING: don't run with debug turned on in production!
26 | DEBUG = True
27 |
28 | ALLOWED_HOSTS = []
29 |
30 |
31 | # Application definition
32 |
33 | INSTALLED_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 | 'blogq',
41 | ]
42 |
43 | MIDDLEWARE_CLASSES = [
44 | 'django.middleware.security.SecurityMiddleware',
45 | 'django.contrib.sessions.middleware.SessionMiddleware',
46 | 'django.middleware.common.CommonMiddleware',
47 | 'django.middleware.csrf.CsrfViewMiddleware',
48 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
49 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
50 | 'django.contrib.messages.middleware.MessageMiddleware',
51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
52 | ]
53 |
54 | ROOT_URLCONF = 'mysite.urls'
55 |
56 | TEMPLATES = [
57 | {
58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
59 | 'DIRS': [],
60 | 'APP_DIRS': True,
61 | 'OPTIONS': {
62 | 'context_processors': [
63 | 'django.template.context_processors.debug',
64 | 'django.template.context_processors.request',
65 | 'django.contrib.auth.context_processors.auth',
66 | 'django.contrib.messages.context_processors.messages',
67 | ],
68 | },
69 | },
70 | ]
71 |
72 | WSGI_APPLICATION = 'mysite.wsgi.application'
73 |
74 |
75 | # Database
76 | # https://docs.djangoproject.com/en/1.9/ref/settings/#databases
77 |
78 | DATABASES = {
79 | 'default': {
80 | 'ENGINE': 'django.db.backends.sqlite3',
81 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
82 | }
83 | }
84 |
85 |
86 | # Password validation
87 | # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
88 |
89 | AUTH_PASSWORD_VALIDATORS = [
90 | {
91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
92 | },
93 | {
94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
95 | },
96 | {
97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
98 | },
99 | {
100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
101 | },
102 | ]
103 |
104 |
105 | # Internationalization
106 | # https://docs.djangoproject.com/en/1.9/topics/i18n/
107 |
108 | LANGUAGE_CODE = 'en-us'
109 |
110 | TIME_ZONE = 'Europe/Kiev'
111 |
112 | USE_I18N = True
113 |
114 | USE_L10N = True
115 |
116 | USE_TZ = True
117 |
118 |
119 | # Static files (CSS, JavaScript, Images)
120 | # https://docs.djangoproject.com/en/1.9/howto/static-files/
121 |
122 | STATIC_URL = '/static/'
123 | STATIC_ROOT = os.path.join(BASE_DIR, 'static')
124 |
--------------------------------------------------------------------------------