Python – Django Girls – Introduction to Django – Video
Months ago, I was planning to take part in the #RailGirls event in Sofia, and I was even a partner in the event. Anyway, as it was postponed up to somewhere in the future, I have decided to go through the DjangoGirls tutorial and to record it. To be honest, I was expecting that the tutorial is “a walk in the park”, but somehow it was a bit more advanced, including JavaScript, Django, OOP Python, SqlLite3, command prompt techniques for adding data to a database and etc.
Was it good? I don’t know, it took me about 3 hours to record it and another 6 to edit it and upload it to YouTube in its final form. Now, when I think about it, I should have read the tutorial before stepping into it. Anyway, this way I have shown that even seasoned developers make rookie mistakes, when they come a bit unprepared into some kind of a project, that they are not currently working on. Thus, all the errors are staying as they were. Plus, I really do not have the desire to edit them out 🙂
Anyway, the whole code is as usually in my GitHub repo here – https://github.com/Vitosh/Python_personal/tree/master/YouTube/002_DjangoGirls.
The core of the project are the following 5 files:
from django.shortcuts import render, redirect
from django.utils import timezone
from blog.form import PostForm
from .models import Post
from django.shortcuts import render, get_object_or_404
#from .forms import PostForm
def post_list(request):
posts = Post.objects.all() #filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/post_list.html', {'posts': posts})
def post_detail(request, 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":
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):
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})
from django.urls import path
from . import views
urlpatterns = [
path('', views.post_list, name='post_list'),
path('post//', views.post_detail, name='post_detail'),
path('post/new/', views.post_new, name='post_new'),
path('post//edit/', views.post_edit, name='post_edit'),
]
{% extends 'blog/base.html' %}
{% block content %}
{% for post in posts %}
<div class="post">
<div class="date">
{{ post.published_date }}
</div>
<h2><a href="">{{ post.title }}</a></h2>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endfor %}
{% endblock %}
from django.conf import settings
from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return "Hello " + self.title
{% load static %}
<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='//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">
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
<h1><a href="/">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>
Enjoy it! 🙂