Skip to content
Open
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 blog/__pycache__/feeds.cpython-311.pyc
Binary file not shown.
Binary file added blog/__pycache__/forms.cpython-311.pyc
Binary file not shown.
Binary file added blog/__pycache__/sitemaps.cpython-311.pyc
Binary file not shown.
23 changes: 23 additions & 0 deletions blog/feeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import markdown
from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords_html
from django.urls import reverse_lazy
from .models import Post


class LatestPostFeed(Feed):
title = 'My blog'
link = reverse_lazy('blog:post_list')
description = 'New posts of my blog.'

def items(self):
return Post.published.all()[:5]

def item_title(self, item):
return item.title

def item_description(self, item):
return truncatewords_html(markdown.markdown(item.body), 30)

def item_pubdate(self, item):
return item.publish
20 changes: 20 additions & 0 deletions blog/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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']


class SearchForm(forms.Form):
query = forms.CharField()
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 4.2 on 2023-05-22 16:57

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'],
},
),
migrations.AddIndex(
model_name='comment',
index=models.Index(fields=['created'], name='blog_commen_created_0e6ed4_idx'),
),
]
20 changes: 20 additions & 0 deletions blog/migrations/0004_post_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2 on 2023-05-24 16:42

from django.db import migrations
import taggit.managers


class Migration(migrations.Migration):

dependencies = [
('taggit', '0005_auto_20220424_2025'),
('blog', '0003_comment_comment_blog_commen_created_0e6ed4_idx'),
]

operations = [
migrations.AddField(
model_name='post',
name='tags',
field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
]
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions blog/sitemaps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib.sitemaps import Sitemap
from .models import Post


class PostSitemap(Sitemap):
changefreq = 'weekly'
priority = 0.9

def items(self):
return Post.published.all()

def lastmod(self, obj):
return obj.updated
11 changes: 11 additions & 0 deletions blog/templates/blog/post/comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'blog/base.html' %}
{% block title %}Add a comment {% endblock title %}

{% 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 content %}
6 changes: 6 additions & 0 deletions blog/templates/blog/post/includes/comment_form.html
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>
7 changes: 7 additions & 0 deletions blog/templates/blog/post/latest_posts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul>
{% for post in latest_posts %}
<li>
<a href="{{post.get_absolute_url}}">{{post.title}}</a>
</li>
{% endfor %}
</ul>
32 changes: 32 additions & 0 deletions blog/templates/blog/post/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends "blog/base.html" %}
{% load blog_tags %}

{% block title %}Search{% endblock %}

{% block content %}
{% if query %}
<h1>Posts containing "{{ query }}"</h1>
<h3>
{% with results.count as total_results %}
Found {{ total_results }} result{{ total_results|pluralize }}
{% endwith %}
</h3>
{% for post in results %}
<h4>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h4>
{{ post.body|markdown|truncatewords_html:12 }}
{% empty %}
<p>There are no results for your query.</p>
{% endfor %}
<p><a href="{% url "blog:post_search" %}">Search again</a></p>
{% else %}
<h1>Search for posts</h1>
<form method="get">
{{ form.as_p }}
<input type="submit" value="Search">
</form>
{% endif %}
{% endblock %}
17 changes: 17 additions & 0 deletions blog/templates/blog/post/share.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'blog/base.html' %}
{% block title %} Share a post {% endblock title %}
{% 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" novalidate>
{{form.as_p }}
{% csrf_token %}
<input type="submit" value="Send e-mail">
</form>
{% endif %}
{% endblock content %}
Empty file added blog/templatetags/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
31 changes: 31 additions & 0 deletions blog/templatetags/blog_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django import template
from ..models import Post
from django.db.models import Count, Q
from django.utils.safestring import mark_safe
import markdown

register = template.Library()


@register.simple_tag
def total_posts():
return Post.published.count()


@register.inclusion_tag('blog/post/latest_posts.html')
def show_latest_posts(count=5):
latest_posts = Post.published.order_by('-publish')[:count]
return {'latest_posts': latest_posts}


@register.simple_tag
def get_most_commented_posts(count=5):
return Post.published.annotate(
total_comments=Count('comments')

).order_by('-total_comments')[:count]


@register.filter(name='markdown')
def markdown_format(text):
return mark_safe(markdown.markdown(text))
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified mysite/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file modified mysite/__pycache__/urls.cpython-311.pyc
Binary file not shown.
20 changes: 16 additions & 4 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


# Application definition

SITE_ID = 1
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
Expand All @@ -38,6 +38,9 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig',
'taggit',
'django.contrib.sites',
'django.contrib.sitemaps',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -76,8 +79,10 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbnew',
'USER': 'postgres',
'PASSWORD': 'Admin@123',
}
}

Expand Down Expand Up @@ -121,7 +126,14 @@
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

# DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# STATICFILES_DIRS = [
# BASE_DIR / "static",
# ]
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'imamitsinghchauhan@gmail.com'
EMAIL_HOST_PASSWORD = 'vbhgqsemhweiuqcx'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
9 changes: 8 additions & 1 deletion mysite/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@

from django.contrib import admin
from django.urls import path, include
from django.contrib.sitemaps.views import sitemap
from blog.sitemaps import PostSitemap
sitemaps = {
'posts': PostSitemap,
}
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls', namespace='blog')),
path('', include('blog.urls', namespace='blog')),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')

]