Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added chapter13/.DS_Store
Binary file not shown.
2 changes: 0 additions & 2 deletions chapter13/author.txt

This file was deleted.

Binary file added chapter13/book_images/django_blog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapter13/book_images/django_splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapter13/book_images/flatpage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions chapter13/cbv/admin.py

This file was deleted.

9 changes: 0 additions & 9 deletions chapter13/cbv/forms.py

This file was deleted.

14 changes: 0 additions & 14 deletions chapter13/cbv/models.py

This file was deleted.

19 changes: 0 additions & 19 deletions chapter13/cbv/signup.html

This file was deleted.

5 changes: 0 additions & 5 deletions chapter13/cbv/urls.py

This file was deleted.

18 changes: 0 additions & 18 deletions chapter13/cbv/views.py

This file was deleted.

2 changes: 0 additions & 2 deletions chapter13/ch.txt

This file was deleted.

Empty file.
3 changes: 3 additions & 0 deletions chapter13/fooblah/blah/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions chapter13/fooblah/blah/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BlahConfig(AppConfig):
name = 'blah'
Empty file.
3 changes: 3 additions & 0 deletions chapter13/fooblah/blah/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
7 changes: 7 additions & 0 deletions chapter13/fooblah/blah/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<h1>This is my base.html file</h1>
I can enter all kinds of strange stuff here
<p>
{% block content %} {% endblock %}
</body></html>
9 changes: 9 additions & 0 deletions chapter13/fooblah/blah/templates/date2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block content %}
it is now {{ thedate }}.
<b>
<p>
<pre>
calculate 2 + 2 = {{ mcalc }}
</pre>
{% endblock %}
5 changes: 5 additions & 0 deletions chapter13/fooblah/blah/templates/flatpages/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
{{ flatpage.content }}
</body>
</html>
3 changes: 3 additions & 0 deletions chapter13/fooblah/blah/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
31 changes: 31 additions & 0 deletions chapter13/fooblah/blah/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.shortcuts import render
from django.template import Template, Context


# Create your views here.
import datetime
from django.shortcuts import HttpResponse


def hello(request):
return HttpResponse("My new web view")


def my_date(request):
now = datetime.datetime.now()
html = "<html><body>The current time is %s.</body></html>" % now
return HttpResponse(html)


def template_my_date(request):
now = datetime.datetime.now()
t = Template("<html><body>It is now {{ tempdate }}.</body></html>")
html = t.render(Context({'tempdate': now}))
return HttpResponse(html)


def d2(request):
calc = 2*2
now = datetime.datetime.now()
my_dict = {'thedate': now, 'mcalc': calc}
return render(None, "date2.html", my_dict)
Empty file.
23 changes: 23 additions & 0 deletions chapter13/fooblah/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.contrib import admin
from blog.models import Comment

# Register your models here.


from blog.models import Post


class PostAdmin(admin.ModelAdmin):
list_display = ["title"]


class CommentAdmin(admin.ModelAdmin):
def post_name(self, instance):
return instance.post.title

list_display = ["author", "post_name"]
search_fields = ["post_name"]


admin.site.register(Post, PostAdmin)
admin.site.register(Comment,CommentAdmin)
5 changes: 5 additions & 0 deletions chapter13/fooblah/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
name = 'blog'
25 changes: 25 additions & 0 deletions chapter13/fooblah/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.0.4 on 2020-03-10 03:55

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=60)),
('body', models.TextField()),
('created', models.DateTimeField(auto_now_add=True)),
('author', models.CharField(default='author', max_length=60)),
('slug', models.CharField(default='author', max_length=60)),
],
),
]
27 changes: 27 additions & 0 deletions chapter13/fooblah/blog/migrations/0002_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.0.4 on 2020-03-12 00:41

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('blog', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('body', models.TextField()),
('approved_comment', models.BooleanField(default=False)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='comments', to='blog.Post')),
],
),
]
Empty file.
31 changes: 31 additions & 0 deletions chapter13/fooblah/blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.db import models

# Create your models here.


class Post(models.Model):
title = models.CharField(max_length=60)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
author = models.CharField(default='author', max_length=60)
slug = models.CharField(default='author', max_length=60)

def __uniode__(self):
return self.title

def __str__(self):
return self.title


class Comment(models.Model):
created = models.DateTimeField(auto_now_add=True)
# author=models.CharField(default='author',max_length=60)
author = models.ForeignKey('auth.User', on_delete=models.DO_NOTHING)
body = models.TextField()
# post=models.ForeignKey(Post)
# post=models.ForeignKey(Post, related_name='comments')
post = models.ForeignKey('blog.post', related_name='comments', on_delete=models.DO_NOTHING)
approved_comment = models.BooleanField(default=False)

def __unicode__(self):
return unicode("%s: %s" % (self.post, self.body[:60]))
24 changes: 24 additions & 0 deletions chapter13/fooblah/blog/templates/blog/post_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'base.html' %}
{% block content %}

<h2>News</h2>
<p><b>Title:{{ post.title }}</b></p>
<p>Slug:{{ post.slug}}</p>
<p>Author:<u> {{ post.author}}</u>
<br>Date:{{ post.created}}</p>
<p></p>
<p>{{ post.body}}</p>

<h2>Comments:</h2>

{% for comment in post.comments.all %}
{% if comment.approved_comment %}
<p>Author: <u>{{ comment.author }}</u>
<br>Comment: {{ comment.body}}</p>
{% endif %}
<p></p>
{% endfor %}

<p><a href="/blog/{{ post.id }}/comment/">Add comment</a>

{% endblock %}
20 changes: 20 additions & 0 deletions chapter13/fooblah/blog/templates/blog/post_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'base.html' %}
{% block content %}
<h2>News</h2>
{% if user.is_staff %}
<a href="{% url 'admin:index' %}">Admin</a>
<a href="{% url 'admin:blog_post_add' %}">Add post</a>
{% endif %}

<p></p>

<ul>
{% for bpost in object_list %}
<li><p>Title: {{ bpost.title }}<br>Slug: {{ bpost.slug }}
<br><a href="/blog/{{ bpost.id }}/">Body</a></li>
{% empty %}
<li>No articles yet.</li>
{% endfor %}
</ul>

{% endblock %}
3 changes: 3 additions & 0 deletions chapter13/fooblah/blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
13 changes: 13 additions & 0 deletions chapter13/fooblah/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from django.urls import path
from blog.views import PostListView
from blog.views import PostDetailView
from blog.views import PostCommentView


urlpatterns = [
path('', PostListView.as_view(), name='post-list'),
path('<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('<int:pk>/comment/', PostCommentView, name='postcomment'),

]
35 changes: 35 additions & 0 deletions chapter13/fooblah/blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.shortcuts import render

# Create your views here.
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from blog.models import Post


class PostListView(ListView):
model = Post
paginate_by = 3
queryset = Post.objects.order_by('-created')


class PostDetailView(DetailView):
model = Post


def PostCommentView(request,pk):
user = User.objects.get(pk=pk)
myedit = Profile.objects.get(user=user)

post = get_object_or_404(Post,pk=pk)
if request.method == 'POST':
form = PostComment(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.author=user
comment.save()
return redirect('/thanks/')
# return redirect('asdf.html',pk=post.pk)
else:
form = PostComment()
return render(request, 'postcomment.html',{'form':form})
Binary file added chapter13/fooblah/db.sqlite3
Binary file not shown.
Empty file.
Loading