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
1020e90
commit 51e3f20
Showing
33 changed files
with
450 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.
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.contrib import admin | ||
from .models import Post | ||
|
||
|
||
@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'] |
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,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')], | ||
}, | ||
), | ||
] |
Empty file.
Binary file added
BIN
+2.2 KB
Module02/mysite/blog/migrations/__pycache__/0001_initial.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+182 Bytes
Module02/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,27 @@ | ||
from django.db import models | ||
from django.utils import timezone | ||
from django.contrib.auth.models import User | ||
|
||
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) | ||
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 |
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,11 @@ | ||
{% 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 }} | ||
{% 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,19 @@ | ||
{% extends "blog/base.html" %} | ||
|
||
{% block title %}My Blog{% endblock %} | ||
|
||
{% block content %} | ||
<h1>My Blog</h1> | ||
{% for post in posts %} | ||
<h2> | ||
<a href="{% url 'blog:post_detail' post.id %}"> | ||
{{ post.title }} | ||
</a> | ||
</h2> | ||
<p class="date"> | ||
Published {{ post.publish }} by {{ post.author }} | ||
</p> | ||
{{ post.body|truncatewords:30|linebreaks }} | ||
<hr /> | ||
{% 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,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
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,9 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
app_name = 'blog' | ||
|
||
urlpatterns = [ | ||
path('', views.post_list, name='post_list'), | ||
path('<int:id>/', views.post_detail, name='post_detail'), | ||
] |
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 @@ | ||
from django.shortcuts import render, get_object_or_404 | ||
from .models import Post | ||
|
||
def post_list(request): | ||
posts = Post.objects.all() | ||
return render(request, 'blog/post/list.html', {'posts': posts}) | ||
|
||
def post_detail(request, id): | ||
post = get_object_or_404(Post, id=id, status=Post.Status.PUBLISHED) | ||
return render(request, 'blog/post/detail.html', {'post': post}) |
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,22 @@ | ||
#!/usr/bin/env python | ||
"""Django's command-line utility for administrative tasks.""" | ||
import os | ||
import sys | ||
|
||
|
||
def main(): | ||
"""Run administrative tasks.""" | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?" | ||
) from exc | ||
execute_from_command_line(sys.argv) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Empty file.
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,16 @@ | ||
""" | ||
ASGI config for mysite project. | ||
It exposes the ASGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') | ||
|
||
application = get_asgi_application() |
Oops, something went wrong.