Showing
4 changed files
with
3 additions
and
73 deletions
1 | from django.db import models | 1 | from django.db import models |
2 | from django.utils import timezone | 2 | from django.utils import timezone |
3 | 3 | ||
4 | - | ||
5 | -class Post(models.Model): | ||
6 | - author = models.ForeignKey('auth.User', on_delete=models.CASCADE) | ||
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 | ... | ... |
1 | from django.conf.urls import url | 1 | from django.conf.urls import url |
2 | from website import views, auth_views | 2 | from website import views, auth_views |
3 | +from django.shortcuts import redirect | ||
3 | 4 | ||
4 | urlpatterns = [ | 5 | urlpatterns = [ |
5 | # blog | 6 | # blog |
6 | - url(r'^$', views.post_list, name='post_list'), | 7 | + url(r'^$', redirect('login')), |
7 | - url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'), | ||
8 | - url(r'^post/new/$', views.post_new, name='post_new'), | ||
9 | - url(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'), | ||
10 | - url(r'^drafts/$', views.post_draft_list, name='post_draft_list'), | ||
11 | - url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'), | ||
12 | - | ||
13 | url(r'^files/', views.file_list, name='file_list'), | 8 | url(r'^files/', views.file_list, name='file_list'), |
14 | url(r'^accounts/signup/$', auth_views.signup, name='signup'), | 9 | url(r'^accounts/signup/$', auth_views.signup, name='signup'), |
15 | url(r'^accounts/delete_account/$', auth_views.delete_account, name='delete_account'), | 10 | url(r'^accounts/delete_account/$', auth_views.delete_account, name='delete_account'), | ... | ... |
... | @@ -9,52 +9,3 @@ from website.forms import PostForm | ... | @@ -9,52 +9,3 @@ from website.forms import PostForm |
9 | def file_list(request): | 9 | def file_list(request): |
10 | files = File.objects.all() | 10 | files = File.objects.all() |
11 | return render(request, 'website/file_list.html', {'files': files}) | 11 | return render(request, 'website/file_list.html', {'files': files}) |
12 | - | ||
13 | - | ||
14 | -def post_list(request): | ||
15 | - posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') | ||
16 | - return render(request, 'website/post_list.html', {'posts':posts}) | ||
17 | - | ||
18 | -def post_detail(request, pk): | ||
19 | - post = get_object_or_404(Post, pk=pk) | ||
20 | - return render(request, 'website/post_detail.html', {'post': post}) | ||
21 | - | ||
22 | -@login_required | ||
23 | -def post_new(request): | ||
24 | - if request.method == "POST": | ||
25 | - form = PostForm(request.POST) | ||
26 | - if form.is_valid(): | ||
27 | - post = form.save(commit=False) | ||
28 | - post.author = request.user | ||
29 | - post.published_date = timezone.now() | ||
30 | - post.save() | ||
31 | - return redirect('post_detail', pk=post.pk) | ||
32 | - else: | ||
33 | - form = PostForm() | ||
34 | - return render(request, 'website/post_edit.html', {'form': form}) | ||
35 | - | ||
36 | -@login_required | ||
37 | -def post_edit(request, pk): | ||
38 | - post = get_object_or_404(Post, pk=pk) | ||
39 | - if request.method == "POST": | ||
40 | - form = PostForm(request.POST, instance=post) | ||
41 | - if form.is_valid(): | ||
42 | - post = form.save(commit=False) | ||
43 | - post.author = request.user | ||
44 | - post.published_date = timezone.now() | ||
45 | - post.save() | ||
46 | - return redirect('post_detail', pk=post.pk) | ||
47 | - else: | ||
48 | - form = PostForm(instance=post) | ||
49 | - return render(request, 'website/post_edit.html', {'form': form}) | ||
50 | - | ||
51 | -@login_required | ||
52 | -def post_draft_list(request): | ||
53 | - posts = Post.objects.filter(published_date__isnull=True).order_by('created_date') | ||
54 | - return render(request, 'website/post_draft_list.html', {'posts': posts}) | ||
55 | - | ||
56 | -@login_required | ||
57 | -def post_publish(request, pk): | ||
58 | - post = get_object_or_404(Post, pk=pk) | ||
59 | - post.publish() | ||
60 | - return redirect('post_detail', pk=pk) | ... | ... |
-
Please register or login to post a comment