forked from yahyaaly151989/Mastering_Django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
921fbb0
commit 55ba561
Showing
43 changed files
with
692 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,18 @@ | ||
from django.contrib import admin | ||
from .models import Post, Comment | ||
|
||
@admin.register(Post) | ||
class PostAdmin(admin.ModelAdmin): | ||
list_display = ['title', 'slug', 'author', 'publish', 'status'] | ||
list_filter = ['status', 'created', 'publish', 'author'] | ||
search_fields = ['title', 'body'] | ||
prepopulated_fields = {'slug': ('title',)} | ||
raw_id_fields = ['author'] | ||
date_hierarchy = 'publish' | ||
ordering = ['status', 'publish'] | ||
|
||
@admin.register(Comment) | ||
class CommentAdmin(admin.ModelAdmin): | ||
list_display = ['name', 'email', 'post', 'created', 'active'] | ||
list_filter = ['active', 'created', 'updated'] | ||
search_fields = ['name', 'email', 'body'] |
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class BlogConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'blog' |
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 @@ | ||
from django import forms | ||
from .models import Comment | ||
|
||
class EmailPostForm(forms.Form): | ||
name = forms.CharField(max_length=25) | ||
email = forms.EmailField() | ||
to = forms.EmailField() | ||
comments = forms.CharField(required=False, widget=forms.Textarea) | ||
|
||
class CommentForm(forms.ModelForm): | ||
class Meta: | ||
model = Comment | ||
fields = ['name', 'email', 'body'] |
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,36 @@ | ||
# Generated by Django 4.2.4 on 2023-08-20 07:26 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Post', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('title', models.CharField(max_length=250)), | ||
('slug', models.SlugField(max_length=250)), | ||
('body', models.TextField()), | ||
('publish', models.DateTimeField(default=django.utils.timezone.now)), | ||
('created', models.DateTimeField(auto_now_add=True)), | ||
('updated', models.DateTimeField(auto_now=True)), | ||
('status', models.CharField(choices=[('DF', 'Draft'), ('PB', 'Published')], default='DF', max_length=2)), | ||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_posts', to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'ordering': ['-publish'], | ||
'indexes': [models.Index(fields=['-publish'], name='blog_post_publish_bb7600_idx')], | ||
}, | ||
), | ||
] |
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,18 @@ | ||
# Generated by Django 4.2.4 on 2023-08-20 11:59 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('blog', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='post', | ||
name='slug', | ||
field=models.SlugField(max_length=250, unique_for_date='publish'), | ||
), | ||
] |
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,31 @@ | ||
# Generated by Django 4.2.4 on 2023-08-20 12:32 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('blog', '0002_alter_post_slug'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Comment', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=80)), | ||
('email', models.EmailField(max_length=254)), | ||
('body', models.TextField()), | ||
('created', models.DateTimeField(auto_now_add=True)), | ||
('updated', models.DateTimeField(auto_now=True)), | ||
('active', models.BooleanField(default=True)), | ||
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.post')), | ||
], | ||
options={ | ||
'ordering': ['created'], | ||
'indexes': [models.Index(fields=['created'], name='blog_commen_created_0e6ed4_idx')], | ||
}, | ||
), | ||
] |
Empty file.
Binary file added
BIN
+2.2 KB
Module03/mysite/blog/migrations/__pycache__/0001_initial.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+833 Bytes
Module03/mysite/blog/migrations/__pycache__/0002_alter_post_slug.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+1.8 KB
Module03/mysite/blog/migrations/__pycache__/0003_comment.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+182 Bytes
Module03/mysite/blog/migrations/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
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,53 @@ | ||
from django.db import models | ||
from django.utils import timezone | ||
from django.contrib.auth.models import User | ||
from django.urls import reverse | ||
|
||
class Post(models.Model): | ||
|
||
class Status(models.TextChoices): | ||
DRAFT = 'DF', 'Draft' | ||
PUBLISHED = 'PB', 'Published' | ||
|
||
title = models.CharField(max_length=250) | ||
slug = models.SlugField(max_length=250, unique_for_date='publish') | ||
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts') | ||
body = models.TextField() | ||
publish = models.DateTimeField(default=timezone.now) | ||
created = models.DateTimeField(auto_now_add=True) | ||
updated = models.DateTimeField(auto_now=True) | ||
status = models.CharField(max_length=2, choices=Status.choices, default=Status.DRAFT) | ||
|
||
class Meta: | ||
ordering = ['-publish'] | ||
indexes = [ | ||
models.Index(fields=['-publish']), | ||
] | ||
|
||
def __str__(self): | ||
return self.title | ||
|
||
def get_absolute_url(self): | ||
return reverse("blog:post_detail", args=[ | ||
self.publish.year, | ||
self.publish.month, | ||
self.publish.day, | ||
self.slug, | ||
]) | ||
|
||
class Comment(models.Model): | ||
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments') | ||
name = models.CharField(max_length=80) | ||
email = models.EmailField() | ||
body = models.TextField() | ||
created = models.DateTimeField(auto_now_add=True) | ||
updated = models.DateTimeField(auto_now=True) | ||
active = models.BooleanField(default=True) | ||
|
||
class Meta: | ||
ordering = ['created'] | ||
indexes = [ | ||
models.Index(fields=['created']), | ||
] | ||
def __str__(self): | ||
return f'Comment by {self.name} on {self.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,92 @@ | ||
body { | ||
margin:0; | ||
padding:0; | ||
font-family:helvetica, sans-serif; | ||
} | ||
|
||
a { | ||
color:#00abff; | ||
text-decoration:none; | ||
} | ||
|
||
h1 { | ||
font-weight:normal; | ||
border-bottom:1px solid #bbb; | ||
padding:0 0 10px 0; | ||
} | ||
|
||
h2 { | ||
font-weight:normal; | ||
margin:30px 0 0; | ||
} | ||
|
||
#content { | ||
float:left; | ||
width:60%; | ||
padding:0 0 0 30px; | ||
} | ||
|
||
#sidebar { | ||
float:right; | ||
width:30%; | ||
padding:10px; | ||
background:#efefef; | ||
height:100%; | ||
} | ||
|
||
p.date { | ||
color:#ccc; | ||
font-family: georgia, serif; | ||
font-size: 12px; | ||
font-style: italic; | ||
} | ||
|
||
/* pagination */ | ||
.pagination { | ||
margin:40px 0; | ||
font-weight:bold; | ||
} | ||
|
||
/* forms */ | ||
label { | ||
float:left; | ||
clear:both; | ||
color:#333; | ||
margin-bottom:4px; | ||
} | ||
input, textarea { | ||
clear:both; | ||
float:left; | ||
margin:0 0 10px; | ||
background:#ededed; | ||
border:0; | ||
padding:6px 10px; | ||
font-size:12px; | ||
} | ||
input[type=submit] { | ||
font-weight:bold; | ||
background:#00abff; | ||
color:#fff; | ||
padding:10px 20px; | ||
font-size:14px; | ||
text-transform:uppercase; | ||
} | ||
.errorlist { | ||
color:#cc0033; | ||
float:left; | ||
clear:both; | ||
padding-left:10px; | ||
} | ||
|
||
/* comments */ | ||
.comment { | ||
padding:10px; | ||
} | ||
.comment:nth-child(even) { | ||
background:#efefef; | ||
} | ||
.comment .info { | ||
font-weight:bold; | ||
font-size:12px; | ||
color:#666; | ||
} |
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,19 @@ | ||
{% load static %} | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>{% block title %}{% endblock %}</title> | ||
<link href="{% static "css/blog.css" %}" rel="stylesheet"> | ||
</head> | ||
<body> | ||
<div id="content"> | ||
{% block content %} | ||
{% endblock %} | ||
</div> | ||
<div id="sidebar"> | ||
<h2>My blog</h2> | ||
<p>This is my blog.</p> | ||
</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,10 @@ | ||
{% extends "blog/base.html" %} | ||
{% block title %}Add a comment{% endblock %} | ||
{% block content %} | ||
{% if comment %} | ||
<h2>Your comment has been added.</h2> | ||
<p><a href="{{ post.get_absolute_url }}">Back to the post</a></p> | ||
{% else %} | ||
{% include "blog/post/includes/comment_form.html" %} | ||
{% endif %} | ||
{% 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,33 @@ | ||
{% extends "blog/base.html" %} | ||
|
||
{% block title %}{{ post.title }}{% endblock %} | ||
|
||
{% block content %} | ||
<h1>{{ post.title }}</h1> | ||
<p class="date"> | ||
Published {{ post.publish }} by {{ post.author }} | ||
</p> | ||
{{ post.body|linebreaks }} | ||
<p> | ||
<a href="{% url "blog:post_share" post.id %}"> | ||
Share this post | ||
</a> | ||
</p> | ||
{% with comments.count as total_comments %} | ||
<h2> | ||
{{ total_comments }} comment{{ total_comments|pluralize }} | ||
</h2> | ||
{% endwith %} | ||
{% for comment in comments %} | ||
<div class="comment"> | ||
<p class="info"> | ||
Comment {{ forloop.counter }} by {{ comment.name }} | ||
{{ comment.created }} | ||
</p> | ||
{{ comment.body|linebreaks }} | ||
</div> | ||
{% empty %} | ||
<p>There are no comments.</p> | ||
{% endfor %} | ||
{% include "blog/post/includes/comment_form.html" %} | ||
{% endblock %} |
6 changes: 6 additions & 0 deletions
6
Module03/mysite/blog/templates/blog/post/includes/comment_form.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,6 @@ | ||
<h2>Add a new comment</h2> | ||
<form action="{% url "blog:post_comment" post.id %}" method="post"> | ||
{{ form.as_p }} | ||
{% csrf_token %} | ||
<p><input type="submit" value="Add comment"></p> | ||
</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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% extends "blog/base.html" %} | ||
|
||
{% block title %}My Blog{% endblock %} | ||
|
||
{% block content %} | ||
<h1>My Blog</h1> | ||
{% for post in posts %} | ||
<h2> | ||
<a href="{{post.get_absolute_url}}"> | ||
{{ post.title }} | ||
</a> | ||
</h2> | ||
<p class="date"> | ||
Published {{ post.publish }} by {{ post.author }} | ||
</p> | ||
{{ post.body|truncatewords:30|linebreaks }} | ||
<hr /> | ||
{% endfor %} | ||
{% include "pagination.html" with page=page_obj %} | ||
{% 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" %} | ||
{% block title %}Share a Post{% endblock %} | ||
{% block content %} | ||
{% if sent %} | ||
<h1>E-mail successfully sent</h1> | ||
<p> | ||
"{{ post.title }}" was successfully sent to {{ form.cleaned_data.to }}. | ||
</p> | ||
{% else %} | ||
<h1>Share "{{ post.title }}" by e-mail</h1> | ||
<form method="post"> | ||
{{ form.as_p }} | ||
{% csrf_token %} | ||
<input type="submit" value="Send e-mail"> | ||
</form> | ||
{% endif %} | ||
{% endblock %} |
Oops, something went wrong.