Browse Source

DjangoGirls Final blog

scadl 6 years ago
parent
commit
e6bd9f1072

+ 7 - 0
blog/forms.py

@@ -0,0 +1,7 @@
+from django import forms
+from .models import Post
+
+class PostForm(forms.ModelForm):
+    class Meta:
+        model = Post
+        fields = ('title', 'text',)

+ 30 - 0
blog/static/css/blog.css

@@ -0,0 +1,30 @@
+.page-header{
+  background: silver;
+  margin-top: 0px;
+  padding: 20px 20px 20px 40px;
+}
+
+.page-header h1,
+.page-header h1 a,
+.page-header h1 a:visited,
+.page-header h1 a:active
+{
+  font-family: 'Galada', cursive;
+  color: white
+}
+
+h2 a,
+h2{
+  font-family: 'Galada', cursive;
+  color: grey;
+}
+
+.content{
+  padding: 15px;
+}
+
+.datelbl{
+  float: right;
+  color: silver;
+  font-style: italic;
+}

+ 33 - 0
blog/templates/blog/base.html

@@ -0,0 +1,33 @@
+{% load staticfiles %}
+<html>
+    <head>
+        <title>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="https://fonts.googleapis.com/css?family=Galada" rel="stylesheet">
+        <link rel="stylesheet" href="{% static 'css/blog.css' %}"
+    </head>
+    <body>
+        <div class="page-header">
+            <h1>
+              <a href="https://en.wikipedia.org/wiki/Django" class="external_link" id="link_to_wiki_page">
+                Django Girls Blog
+              </a>
+              {% if user.is_authenticated %}
+              <a href="{% url 'post_new' %}">
+                <span class="glyphicon glyphicon-plus"></span>
+              </a>
+              {% endif %}
+            </h1>
+        </div>
+
+        <div class="content">
+        {% block content %}
+        {% endblock %}
+      </div>
+
+      <div style="text-align:center">
+        <a href="/admin" target="_blank">Admin UI</a>
+      </div>
+    </body>
+</html>

+ 15 - 0
blog/templates/blog/post_detail.html

@@ -0,0 +1,15 @@
+{% extends 'blog/base.html' %}
+
+{% block content %}
+          {% if post.published_date %}
+          <p class="datelbl"> {{ post.published_date }} </p>
+          {% endif %}
+          {% if user.is_authenticated %}
+          <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}" style="float:right">
+            <span class="glyphicon glyphicon-pencil"></span>
+          </a>
+          {% endif %}
+          <h2> {{ post.title }} </h2>
+          <p> {{ post.text|linebreaksbr }} </p>
+          <hr>
+{% endblock %}

+ 10 - 0
blog/templates/blog/post_edit.html

@@ -0,0 +1,10 @@
+{% extends 'blog/base.html' %}
+
+{% block content %}
+  <h2>New Post</h2>
+  <form method="POST" class="post-form">
+  {% csrf_token %}
+  {{ form.as_p }}
+  <button type="submit" class="save btn btn-default">Save</button>
+  </form>
+{% endblock %}

+ 7 - 13
blog/templates/blog/post_list.html

@@ -1,18 +1,12 @@
-<html>
-    <head>
-        <title>Django Girls blog</title>
-    </head>
-    <body>
-        <div>
-            <h1><a href="/">Django Girls Blog</a></h1>
-        </div>
+{% extends 'blog/base.html' %}
 
 
+{% block content %}
         {% for post in posts %}
         {% for post in posts %}
-        <div>
-          <p> <i>Published:</i> {{ post.published_date }} </p>
-          <h2> <a href="">{{ post.title }}</a> </h2>
+        <div style="padding-bottom:10px">
+          <p class="datelbl"> {{ post.published_date }} </p>
+          <h2> <a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a> </h2>
           <p> {{ post.text|linebreaksbr }} </p>
           <p> {{ post.text|linebreaksbr }} </p>
+          <hr>
         </div>
         </div>
         {% endfor %}
         {% endfor %}
-    </body>
-</html>
+{% endblock %}

+ 4 - 0
blog/urls.py

@@ -3,4 +3,8 @@ from . import views
 
 
 urlpatterns = [
 urlpatterns = [
     url(r'^$', views.post_list, name='post_list'),
     url(r'^$', views.post_list, name='post_list'),
+    url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
+    url(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'),
+    url(r'^post/new/$', views.post_new, name='post_new'),
+
 ]
 ]

+ 38 - 1
blog/views.py

@@ -1,8 +1,45 @@
-from django.shortcuts import render
+from django.shortcuts import render, get_object_or_404, redirect
 from django.utils import timezone
 from django.utils import timezone
 from .models import Post
 from .models import Post
+from .forms import PostForm
+
 
 
 # Create your views here.
 # Create your views here.
 def post_list(request):
 def post_list(request):
     posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
     posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
     return render(request, 'blog/post_list.html', {'posts':posts})
     return render(request, 'blog/post_list.html', {'posts':posts})
+
+def post_detail(request, pk):
+    # Failsafe version of:
+    # post = Post.object.get(pk=pk)
+    post = get_object_or_404(Post, pk=pk)
+    return render(request, 'blog/post_detail.html', {'post':post})
+
+def post_new(request):
+    if request.method == "POST":
+        # filled post details and submited
+        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:
+        # Just opened new_post page
+        form = PostForm()
+    return render(request, 'blog/post_edit.html', {'form':form})
+
+def post_edit(request, pk):
+    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})