-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create form for create and update post
- Loading branch information
Showing
11 changed files
with
211 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
# Register your models here. | ||
from django.contrib import admin | ||
|
||
from .models import Post | ||
|
||
admin.site.register(Post) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Creating and editing forms.""" | ||
from django import forms | ||
|
||
from .models import Post | ||
|
||
|
||
class PostForm(forms.ModelForm): | ||
"""Class form based on the model.""" | ||
|
||
class Meta: | ||
"""Description metadata.""" | ||
|
||
model = Post | ||
fields = ('title', 'text',) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
.page-header { | ||
background-color: #ff9400; | ||
margin-top: 0; | ||
padding: 20px 20px 20px 40px; | ||
} | ||
|
||
.page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active { | ||
color: #ffffff; | ||
font-size: 36pt; | ||
text-decoration: none; | ||
} | ||
|
||
.content { | ||
margin-left: 40px; | ||
} | ||
|
||
h1, h2, h3, h4 { | ||
font-family: 'Lobster', cursive; | ||
} | ||
|
||
.date { | ||
color: #828282; | ||
} | ||
|
||
.save { | ||
float: right; | ||
} | ||
|
||
.post-form textarea, .post-form input { | ||
width: 100%; | ||
} | ||
|
||
.top-menu, .top-menu:hover, .top-menu:visited { | ||
color: #ffffff; | ||
float: right; | ||
font-size: 26pt; | ||
margin-right: 20px; | ||
} | ||
|
||
.post { | ||
margin-bottom: 70px; | ||
} | ||
|
||
.post h1 a, .post h1 a:visited { | ||
color: #000000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{% load staticfiles %} | ||
|
||
{% load i18n %} | ||
|
||
<html> | ||
<head> | ||
<title>{% trans "Django Girls blog" %}</title> | ||
<link rel="stylesheet" | ||
href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> | ||
<link rel="stylesheet" | ||
href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"> | ||
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" | ||
rel="stylesheet" type="text/css"> | ||
<link rel="stylesheet" href="{% static 'css/blog.css' %}"> | ||
</head> | ||
<body> | ||
<div class="page-header"> | ||
{% if user.is_authenticated %} | ||
<a href="{% url 'post_new' %}" class="top-menu"> | ||
<span class="glyphicon glyphicon-plus"></span> | ||
</a> | ||
{% endif %} | ||
<h1><a href="/">{% trans "Django Girls Blog:" %}</a></h1> | ||
</div> | ||
<div class="content container"> | ||
<div class="row"> | ||
<div class="col-md-8"> | ||
{% block content %} | ||
{% endblock %} | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% extends "blog/base.html" %} | ||
|
||
{% load i18n %} | ||
|
||
{% block content %} | ||
<div class="post"> | ||
{% if user.is_authenticated %} | ||
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"> | ||
<span class="glyphicon glyphicon-pencil"></span> | ||
</a> | ||
{% endif %} | ||
{% if post.published_date %} | ||
<div class="date"> | ||
{{ post.published_date }} | ||
</div> | ||
{% endif %} | ||
<h1>{{ post.title }}</h1> | ||
<p>{{ post.text|linebreaksbr }}</p> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% extends "blog/base.html" %} | ||
|
||
{% load i18n %} | ||
|
||
{% block content %} | ||
<h1>New post</h1> | ||
<form method="POST" class="post-form">{% csrf_token %} | ||
{{ form.as_p }} | ||
<button type="submit" class="save btn btn-default"> | ||
{% trans "Save" %} | ||
</button> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% extends "blog/base.html" %} | ||
|
||
{% load i18n %} | ||
|
||
{% block content %} | ||
{% for post in posts %} | ||
<div class="post"> | ||
<div class="date"> | ||
<p>{% trans "Posted on:" %} {{ post.published_date }}</p> | ||
</div> | ||
<h1> | ||
<a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a> | ||
</h1> | ||
<p>{{ post.text|linebreaksbr }}</p> | ||
</div> | ||
{% endfor %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""URLs for the blog app.""" | ||
from django.urls import re_path | ||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
re_path(r'^$', views.post_list, name='post_list'), | ||
re_path(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'), | ||
re_path(r'^post/new/$', views.post_new, name='post_new'), | ||
re_path(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,53 @@ | ||
"""Views for the blog app.""" | ||
from django.shortcuts import get_object_or_404, redirect, render | ||
from django.utils import timezone | ||
|
||
# Create your views here. | ||
from .forms import PostForm | ||
from .models import Post | ||
|
||
|
||
def post_list(request): | ||
"""Show list posts on the page.""" | ||
posts = ( | ||
Post.objects. | ||
filter(published_date__lte=timezone.now()). | ||
order_by('-published_date') | ||
) | ||
return render(request, 'blog/post_list.html', {'posts': posts}) | ||
|
||
|
||
def post_detail(request, pk): | ||
"""Show the full post on the page.""" | ||
post = get_object_or_404(Post, pk=pk) | ||
return render(request, 'blog/post_detail.html', {'post': post}) | ||
|
||
|
||
def post_new(request): | ||
"""Create new post.""" | ||
if request.method == 'POST': | ||
form = PostForm(request.POST) | ||
if form.is_valid(): | ||
post = form.save(commit=False) | ||
post.author = request.user | ||
post.published_date = timezone.now() | ||
post.save() | ||
return redirect('post_detail', pk=post.pk) | ||
else: | ||
form = PostForm() | ||
return render(request, 'blog/post_edit.html', {'form': form}) | ||
|
||
|
||
def post_edit(request, pk): | ||
"""Edit post.""" | ||
post = get_object_or_404(Post, pk=pk) | ||
if request.method == 'POST': | ||
form = PostForm(request.POST, instance=post) | ||
if form.is_valid(): | ||
post = form.save(commit=False) | ||
post.author = request.user | ||
post.published_date = timezone.now() | ||
post.save() | ||
return redirect('post_detail', pk=post.pk) | ||
else: | ||
form = PostForm(instance=post) | ||
return render(request, 'blog/post_edit.html', {'form': form}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
"""mysite URL Configuration.""" | ||
from django.contrib import admin | ||
from django.urls import path | ||
from django.urls import include, path | ||
|
||
urlpatterns = [ | ||
path('admin/', admin.site.urls), | ||
path('', include('blog.urls')), | ||
] |