├── blog
├── __init__.py
├── migrations
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-310.pyc
│ │ ├── __init__.cpython-38.pyc
│ │ ├── 0001_initial.cpython-310.pyc
│ │ └── 0001_initial.cpython-38.pyc
│ └── 0001_initial.py
├── tests.py
├── __pycache__
│ ├── admin.cpython-38.pyc
│ ├── apps.cpython-310.pyc
│ ├── apps.cpython-38.pyc
│ ├── urls.cpython-310.pyc
│ ├── urls.cpython-38.pyc
│ ├── views.cpython-38.pyc
│ ├── admin.cpython-310.pyc
│ ├── models.cpython-310.pyc
│ ├── models.cpython-38.pyc
│ ├── views.cpython-310.pyc
│ ├── __init__.cpython-310.pyc
│ └── __init__.cpython-38.pyc
├── admin.py
├── apps.py
├── models.py
├── urls.py
└── views.py
├── users
├── __init__.py
├── migrations
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── __init__.cpython-310.pyc
│ │ ├── 0001_initial.cpython-310.pyc
│ │ ├── 0001_initial.cpython-38.pyc
│ │ ├── 0002_profile.cpython-310.pyc
│ │ ├── 0002_profile.cpython-38.pyc
│ │ ├── 0005_alter_profile_user.cpython-38.pyc
│ │ ├── 0005_alter_profile_user.cpython-310.pyc
│ │ ├── 0006_alter_profile_image.cpython-310.pyc
│ │ ├── 0006_alter_profile_image.cpython-38.pyc
│ │ ├── 0007_alter_profile_image.cpython-310.pyc
│ │ ├── 0007_alter_profile_image.cpython-38.pyc
│ │ ├── 0008_alter_profile_image.cpython-310.pyc
│ │ ├── 0008_alter_profile_image.cpython-38.pyc
│ │ ├── 0003_rename_username_user_name.cpython-38.pyc
│ │ ├── 0004_rename_name_user_username.cpython-38.pyc
│ │ ├── 0003_rename_username_user_name.cpython-310.pyc
│ │ └── 0004_rename_name_user_username.cpython-310.pyc
│ ├── 0003_rename_username_user_name.py
│ ├── 0004_rename_name_user_username.py
│ ├── 0007_alter_profile_image.py
│ ├── 0006_alter_profile_image.py
│ ├── 0008_alter_profile_image.py
│ ├── 0005_alter_profile_user.py
│ ├── 0002_profile.py
│ └── 0001_initial.py
├── tests.py
├── static
│ ├── images
│ │ ├── avt.png
│ │ └── code.gif
│ └── css
│ │ └── home.css
├── __pycache__
│ ├── apps.cpython-38.pyc
│ ├── admin.cpython-310.pyc
│ ├── admin.cpython-38.pyc
│ ├── apps.cpython-310.pyc
│ ├── forms.cpython-310.pyc
│ ├── forms.cpython-38.pyc
│ ├── models.cpython-38.pyc
│ ├── views.cpython-310.pyc
│ ├── views.cpython-38.pyc
│ ├── __init__.cpython-310.pyc
│ ├── __init__.cpython-38.pyc
│ ├── models.cpython-310.pyc
│ ├── signals.cpython-310.pyc
│ └── signals.cpython-38.pyc
├── admin.py
├── apps.py
├── templates
│ └── users
│ │ ├── logout.html
│ │ ├── login.html
│ │ ├── register.html
│ │ ├── profile.html
│ │ ├── base.html
│ │ └── home.html
├── signals.py
├── forms.py
├── models.py
└── views.py
├── trydjango
├── __init__.py
├── __pycache__
│ ├── urls.cpython-310.pyc
│ ├── urls.cpython-38.pyc
│ ├── wsgi.cpython-310.pyc
│ ├── wsgi.cpython-38.pyc
│ ├── __init__.cpython-310.pyc
│ ├── __init__.cpython-38.pyc
│ ├── settings.cpython-310.pyc
│ └── settings.cpython-38.pyc
├── asgi.py
├── wsgi.py
├── urls.py
└── settings.py
├── pass.py
├── db.sqlite3
├── media
├── default.png
└── profile_pics
│ └── IMG-20191124-WA0019.jpg
├── templates
└── blog
│ ├── about.html
│ ├── post_form.html
│ ├── post_confirm_delete.html
│ ├── home.html
│ ├── post_detail.html
│ └── base.html
├── manage.py
├── static
└── blog
│ └── main.css
└── Django_Tut.txt
/blog/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/users/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/trydjango/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/blog/migrations/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/users/migrations/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/pass.py:
--------------------------------------------------------------------------------
1 | host_user= 'arjunsdhav@gmail.com'
2 | host_pass = 'sloseauatgkeqwjb'
--------------------------------------------------------------------------------
/db.sqlite3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/db.sqlite3
--------------------------------------------------------------------------------
/blog/tests.py:
--------------------------------------------------------------------------------
1 | from django.test import TestCase
2 |
3 | # Create your tests here.
4 |
--------------------------------------------------------------------------------
/users/tests.py:
--------------------------------------------------------------------------------
1 | from django.test import TestCase
2 |
3 | # Create your tests here.
4 |
--------------------------------------------------------------------------------
/media/default.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/media/default.png
--------------------------------------------------------------------------------
/users/static/images/avt.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/static/images/avt.png
--------------------------------------------------------------------------------
/users/static/images/code.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/static/images/code.gif
--------------------------------------------------------------------------------
/templates/blog/about.html:
--------------------------------------------------------------------------------
1 | {% extends "blog/base.html" %} {% block content %}
2 |
About Page
3 | {% endblock content %}
--------------------------------------------------------------------------------
/blog/__pycache__/admin.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/admin.cpython-38.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/apps.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/apps.cpython-310.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/apps.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/apps.cpython-38.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/urls.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/urls.cpython-310.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/urls.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/urls.cpython-38.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/views.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/views.cpython-38.pyc
--------------------------------------------------------------------------------
/users/__pycache__/apps.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/apps.cpython-38.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/admin.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/admin.cpython-310.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/models.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/models.cpython-310.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/models.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/models.cpython-38.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/views.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/views.cpython-310.pyc
--------------------------------------------------------------------------------
/users/__pycache__/admin.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/admin.cpython-310.pyc
--------------------------------------------------------------------------------
/users/__pycache__/admin.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/admin.cpython-38.pyc
--------------------------------------------------------------------------------
/users/__pycache__/apps.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/apps.cpython-310.pyc
--------------------------------------------------------------------------------
/users/__pycache__/forms.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/forms.cpython-310.pyc
--------------------------------------------------------------------------------
/users/__pycache__/forms.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/forms.cpython-38.pyc
--------------------------------------------------------------------------------
/users/__pycache__/models.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/models.cpython-38.pyc
--------------------------------------------------------------------------------
/users/__pycache__/views.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/views.cpython-310.pyc
--------------------------------------------------------------------------------
/users/__pycache__/views.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/views.cpython-38.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/__init__.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/__init__.cpython-310.pyc
--------------------------------------------------------------------------------
/blog/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/blog/admin.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 | from .models import Post
3 |
4 | # Register your models here.
5 | admin.site.register(Post)
--------------------------------------------------------------------------------
/media/profile_pics/IMG-20191124-WA0019.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/media/profile_pics/IMG-20191124-WA0019.jpg
--------------------------------------------------------------------------------
/trydjango/__pycache__/urls.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/trydjango/__pycache__/urls.cpython-310.pyc
--------------------------------------------------------------------------------
/trydjango/__pycache__/urls.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/trydjango/__pycache__/urls.cpython-38.pyc
--------------------------------------------------------------------------------
/trydjango/__pycache__/wsgi.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/trydjango/__pycache__/wsgi.cpython-310.pyc
--------------------------------------------------------------------------------
/trydjango/__pycache__/wsgi.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/trydjango/__pycache__/wsgi.cpython-38.pyc
--------------------------------------------------------------------------------
/users/__pycache__/__init__.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/__init__.cpython-310.pyc
--------------------------------------------------------------------------------
/users/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/users/__pycache__/models.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/models.cpython-310.pyc
--------------------------------------------------------------------------------
/users/__pycache__/signals.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/signals.cpython-310.pyc
--------------------------------------------------------------------------------
/users/__pycache__/signals.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/__pycache__/signals.cpython-38.pyc
--------------------------------------------------------------------------------
/trydjango/__pycache__/__init__.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/trydjango/__pycache__/__init__.cpython-310.pyc
--------------------------------------------------------------------------------
/trydjango/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/trydjango/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/trydjango/__pycache__/settings.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/trydjango/__pycache__/settings.cpython-310.pyc
--------------------------------------------------------------------------------
/trydjango/__pycache__/settings.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/trydjango/__pycache__/settings.cpython-38.pyc
--------------------------------------------------------------------------------
/users/admin.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 | from .models import Profile
3 | # Register your models here.
4 |
5 | admin.site.register(Profile)
--------------------------------------------------------------------------------
/blog/migrations/__pycache__/__init__.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/migrations/__pycache__/__init__.cpython-310.pyc
--------------------------------------------------------------------------------
/blog/migrations/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/migrations/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/__init__.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/__init__.cpython-310.pyc
--------------------------------------------------------------------------------
/blog/migrations/__pycache__/0001_initial.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/migrations/__pycache__/0001_initial.cpython-310.pyc
--------------------------------------------------------------------------------
/blog/migrations/__pycache__/0001_initial.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/blog/migrations/__pycache__/0001_initial.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0001_initial.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0001_initial.cpython-310.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0001_initial.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0001_initial.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0002_profile.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0002_profile.cpython-310.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0002_profile.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0002_profile.cpython-38.pyc
--------------------------------------------------------------------------------
/blog/apps.py:
--------------------------------------------------------------------------------
1 | from django.apps import AppConfig
2 |
3 |
4 | class BlogConfig(AppConfig):
5 | default_auto_field = 'django.db.models.BigAutoField'
6 | name = 'blog'
7 |
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0005_alter_profile_user.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0005_alter_profile_user.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0005_alter_profile_user.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0005_alter_profile_user.cpython-310.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0006_alter_profile_image.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0006_alter_profile_image.cpython-310.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0006_alter_profile_image.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0006_alter_profile_image.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0007_alter_profile_image.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0007_alter_profile_image.cpython-310.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0007_alter_profile_image.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0007_alter_profile_image.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0008_alter_profile_image.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0008_alter_profile_image.cpython-310.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0008_alter_profile_image.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0008_alter_profile_image.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0003_rename_username_user_name.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0003_rename_username_user_name.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0004_rename_name_user_username.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0004_rename_name_user_username.cpython-38.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0003_rename_username_user_name.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0003_rename_username_user_name.cpython-310.pyc
--------------------------------------------------------------------------------
/users/migrations/__pycache__/0004_rename_name_user_username.cpython-310.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arjundhav/Django_blog/master/users/migrations/__pycache__/0004_rename_name_user_username.cpython-310.pyc
--------------------------------------------------------------------------------
/users/apps.py:
--------------------------------------------------------------------------------
1 | from django.apps import AppConfig
2 |
3 |
4 | class UsersConfig(AppConfig):
5 | default_auto_field = 'django.db.models.BigAutoField'
6 | name = 'users'
7 |
8 | def ready(self):
9 | import users.signals
10 |
--------------------------------------------------------------------------------
/users/templates/users/logout.html:
--------------------------------------------------------------------------------
1 | {% extends "users/home.html" %} {% block content %}
2 | You have been Logged Out
3 |
6 | {% endblock content %}
--------------------------------------------------------------------------------
/trydjango/asgi.py:
--------------------------------------------------------------------------------
1 | """
2 | ASGI config for trydjango project.
3 |
4 | It exposes the ASGI callable as a module-level variable named ``application``.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
8 | """
9 |
10 | import os
11 |
12 | from django.core.asgi import get_asgi_application
13 |
14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trydjango.settings')
15 |
16 | application = get_asgi_application()
17 |
--------------------------------------------------------------------------------
/trydjango/wsgi.py:
--------------------------------------------------------------------------------
1 | """
2 | WSGI config for trydjango project.
3 |
4 | It exposes the WSGI callable as a module-level variable named ``application``.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
8 | """
9 |
10 | import os
11 |
12 | from django.core.wsgi import get_wsgi_application
13 |
14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trydjango.settings')
15 |
16 | application = get_wsgi_application()
17 |
--------------------------------------------------------------------------------
/users/migrations/0003_rename_username_user_name.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-18 16:28
2 |
3 | from django.db import migrations
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | dependencies = [
9 | ('users', '0002_profile'),
10 | ]
11 |
12 | operations = [
13 | migrations.RenameField(
14 | model_name='user',
15 | old_name='username',
16 | new_name='name',
17 | ),
18 | ]
19 |
--------------------------------------------------------------------------------
/users/migrations/0004_rename_name_user_username.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-18 16:31
2 |
3 | from django.db import migrations
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | dependencies = [
9 | ('users', '0003_rename_username_user_name'),
10 | ]
11 |
12 | operations = [
13 | migrations.RenameField(
14 | model_name='user',
15 | old_name='name',
16 | new_name='username',
17 | ),
18 | ]
19 |
--------------------------------------------------------------------------------
/users/migrations/0007_alter_profile_image.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-20 09:58
2 |
3 | from django.db import migrations, models
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | dependencies = [
9 | ('users', '0006_alter_profile_image'),
10 | ]
11 |
12 | operations = [
13 | migrations.AlterField(
14 | model_name='profile',
15 | name='image',
16 | field=models.ImageField(upload_to='profile_pics/'),
17 | ),
18 | ]
19 |
--------------------------------------------------------------------------------
/users/migrations/0006_alter_profile_image.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-20 03:21
2 |
3 | from django.db import migrations, models
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | dependencies = [
9 | ('users', '0005_alter_profile_user'),
10 | ]
11 |
12 | operations = [
13 | migrations.AlterField(
14 | model_name='profile',
15 | name='image',
16 | field=models.ImageField(default='default.jpg', upload_to='profile_pics/'),
17 | ),
18 | ]
19 |
--------------------------------------------------------------------------------
/users/migrations/0008_alter_profile_image.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-25 15:56
2 |
3 | from django.db import migrations, models
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | dependencies = [
9 | ('users', '0007_alter_profile_image'),
10 | ]
11 |
12 | operations = [
13 | migrations.AlterField(
14 | model_name='profile',
15 | name='image',
16 | field=models.ImageField(default='default.png', upload_to='profile_pics/'),
17 | ),
18 | ]
19 |
--------------------------------------------------------------------------------
/templates/blog/post_form.html:
--------------------------------------------------------------------------------
1 | {% extends "blog/base.html" %} {% load crispy_forms_tags %} {% block content %}
2 |
14 | {% endblock content %}
--------------------------------------------------------------------------------
/blog/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 | from django.utils import timezone
3 | from django.contrib.auth.models import User
4 | from django.urls import reverse
5 |
6 |
7 | class Post(models.Model):
8 | title = models.CharField(max_length=100)
9 | content = models.TextField()
10 | date_posted = models.DateTimeField(default=timezone.now)
11 | author = models.ForeignKey(User, on_delete=models.CASCADE)
12 |
13 | def __str__(self):
14 | return self.title
15 |
16 | def get_absolute_url(self):
17 | return reverse('post-detail', kwargs={'pk': self.pk})
--------------------------------------------------------------------------------
/users/signals.py:
--------------------------------------------------------------------------------
1 | # Django's “signal dispatcher” which helps decoupled(independent) applications get notified when actions occur elsewhere in the framework.
2 | from django.db.models.signals import post_save
3 | from django.contrib.auth.models import User
4 | from django.dispatch import receiver
5 | from .models import Profile
6 |
7 | @receiver(post_save, sender=User)
8 | def create_profile(sender, instance, created, **kwargs):
9 | if created:
10 | Profile.objects.create(user=instance)
11 |
12 | @receiver(post_save, sender=User)
13 | def create_profile(sender,instance, **kwargs):
14 | instance.profile.save()
15 |
16 |
--------------------------------------------------------------------------------
/blog/urls.py:
--------------------------------------------------------------------------------
1 | from django.urls import path
2 | from .views import (
3 | PostListView,
4 | PostDetailView,
5 | PostCreateView,
6 | PostUpdateView,
7 | PostDeleteView
8 | )
9 | from . import views
10 |
11 | urlpatterns = [
12 | path('', PostListView.as_view(), name='blog-home'),
13 | path('post//', PostDetailView.as_view(), name='post-detail'),
14 | path('post/new/', PostCreateView.as_view(), name='post-create'),
15 | path('post//update/', PostUpdateView.as_view(), name='post-update'),
16 | path('post//delete/', PostDeleteView.as_view(), name='post-delete'),
17 | path('about/', views.about, name='blog-about'),
18 | ]
--------------------------------------------------------------------------------
/templates/blog/post_confirm_delete.html:
--------------------------------------------------------------------------------
1 | {% extends "blog/base.html" %} {% block content %}
2 |
15 | {% endblock content %}
--------------------------------------------------------------------------------
/users/migrations/0005_alter_profile_user.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-19 03:13
2 |
3 | from django.conf import settings
4 | from django.db import migrations, models
5 | import django.db.models.deletion
6 |
7 |
8 | class Migration(migrations.Migration):
9 |
10 | dependencies = [
11 | migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12 | ('users', '0004_rename_name_user_username'),
13 | ]
14 |
15 | operations = [
16 | migrations.AlterField(
17 | model_name='profile',
18 | name='user',
19 | field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
20 | ),
21 | ]
22 |
--------------------------------------------------------------------------------
/users/forms.py:
--------------------------------------------------------------------------------
1 | from django import forms
2 | from django.contrib.auth.models import User
3 | from django.contrib.auth.forms import UserCreationForm
4 | from .models import Profile
5 |
6 | class UserRegisterForm(UserCreationForm):
7 | email = forms.EmailField()
8 |
9 | class Meta:
10 | model = User
11 | fields = ['username','email','password1','password2']
12 |
13 |
14 | class UserUpdateForm(forms.ModelForm):
15 | email = forms.EmailField()
16 |
17 | class Meta:
18 | model = User
19 | fields = ['username', 'email']
20 |
21 | class ProfileUpdateForm(forms.ModelForm):
22 |
23 | class Meta:
24 | model = Profile
25 | fields = ['image']
26 |
27 |
--------------------------------------------------------------------------------
/users/templates/users/login.html:
--------------------------------------------------------------------------------
1 | {% extends "users/home.html" %} {% load crispy_forms_tags %} {% block content %}
2 |
3 |
18 | {% endblock content %}
--------------------------------------------------------------------------------
/users/templates/users/register.html:
--------------------------------------------------------------------------------
1 | {% extends "users/home.html" %} {% load crispy_forms_tags %} {% block content %}
2 |
3 |
13 |
14 |
Already Have an Account Log In
15 |
16 |
17 | {% endblock content %}
--------------------------------------------------------------------------------
/manage.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | """Django's command-line utility for administrative tasks."""
3 | import os
4 | import sys
5 |
6 |
7 | def main():
8 | """Run administrative tasks."""
9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trydjango.settings')
10 | try:
11 | from django.core.management import execute_from_command_line
12 | except ImportError as exc:
13 | raise ImportError(
14 | "Couldn't import Django. Are you sure it's installed and "
15 | "available on your PYTHONPATH environment variable? Did you "
16 | "forget to activate a virtual environment?"
17 | ) from exc
18 | execute_from_command_line(sys.argv)
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/users/migrations/0002_profile.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-18 16:19
2 |
3 | from django.db import migrations, models
4 | import django.db.models.deletion
5 |
6 |
7 | class Migration(migrations.Migration):
8 |
9 | dependencies = [
10 | ('users', '0001_initial'),
11 | ]
12 |
13 | operations = [
14 | migrations.CreateModel(
15 | name='Profile',
16 | fields=[
17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18 | ('image', models.ImageField(default='default.jpg', upload_to='profile_pics')),
19 | ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='users.user')),
20 | ],
21 | ),
22 | ]
23 |
--------------------------------------------------------------------------------
/trydjango/urls.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 | from django.contrib.auth import views as auth_views
3 | from django.urls import path, include
4 | from users import views as user_views
5 | from blog import views as blog_views
6 | from django.conf import settings
7 | from django.conf.urls.static import static
8 |
9 | urlpatterns = [
10 | path('admin/', admin.site.urls),
11 |
12 | path('', blog_views.home_view, name='home'),
13 | path('register/',user_views.register, name='register'),
14 | path('profile/',user_views.profile, name='profile'),
15 | path('login/',auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
16 | path('logout/',auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
17 | path('', include('blog.urls')),
18 |
19 | ]
20 |
21 | urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
--------------------------------------------------------------------------------
/users/migrations/0001_initial.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-18 14:32
2 |
3 | from django.db import migrations, models
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | initial = True
9 |
10 | dependencies = [
11 | ]
12 |
13 | operations = [
14 | migrations.CreateModel(
15 | name='User',
16 | fields=[
17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18 | ('username', models.CharField(max_length=100)),
19 | ('email', models.EmailField(max_length=254, unique=True)),
20 | ('password1', models.CharField(max_length=300)),
21 | ('password2', models.CharField(max_length=300)),
22 | ('mobile', models.CharField(max_length=15, null=True)),
23 | ],
24 | ),
25 | ]
26 |
--------------------------------------------------------------------------------
/templates/blog/home.html:
--------------------------------------------------------------------------------
1 | {% extends "blog/base.html" %} {% block content %} {% for post in posts %}
2 |
3 |
4 |
5 |
6 |
7 |
11 |
12 |
{{ post.title }} Edit
13 |
14 |
{{ post.content }}
15 |
16 |
17 |
18 |
19 |
20 | {% endfor %} {% endblock content %}
--------------------------------------------------------------------------------
/templates/blog/post_detail.html:
--------------------------------------------------------------------------------
1 | {% extends "blog/base.html" %} {% block content %}
2 |
3 |
4 |
5 |
6 |
{{ object.author }}
7 |
{{ object.date_posted|date:"F d, Y" }} {% if object.author == user %}
8 |
12 | {% endif %}
13 |
14 |
{{ object.title }}
15 |
{{ object.content }}
16 |
17 |
18 | {% endblock content %}
--------------------------------------------------------------------------------
/users/templates/users/profile.html:
--------------------------------------------------------------------------------
1 | {% extends "users/home.html" %} {% load crispy_forms_tags %} {% block content %}
2 |
24 |
25 | {% endblock content %}
--------------------------------------------------------------------------------
/blog/migrations/0001_initial.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 3.2.4 on 2021-09-25 15:56
2 |
3 | from django.conf import settings
4 | from django.db import migrations, models
5 | import django.db.models.deletion
6 | import django.utils.timezone
7 |
8 |
9 | class Migration(migrations.Migration):
10 |
11 | initial = True
12 |
13 | dependencies = [
14 | migrations.swappable_dependency(settings.AUTH_USER_MODEL),
15 | ]
16 |
17 | operations = [
18 | migrations.CreateModel(
19 | name='Post',
20 | fields=[
21 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
22 | ('title', models.CharField(max_length=100)),
23 | ('content', models.TextField()),
24 | ('date_posted', models.DateTimeField(default=django.utils.timezone.now)),
25 | ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
26 | ],
27 | ),
28 | ]
29 |
--------------------------------------------------------------------------------
/users/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 | from django.contrib.auth.models import User as Authuser
3 | from PIL import Image #pillow lib
4 | # Create your models here.
5 |
6 |
7 | class User(models.Model):
8 |
9 | username = models.CharField(max_length=100)
10 | email = models.EmailField(unique=True)
11 | password1 = models.CharField(max_length=300)
12 | password2 = models.CharField(max_length=300)
13 | mobile = models.CharField(max_length=15, null=True)
14 |
15 | def __str__(self):
16 | return self.username
17 |
18 | class Profile(models.Model):
19 | user = models.OneToOneField(Authuser, on_delete=models.CASCADE)
20 | image= models.ImageField(default='default.png',upload_to='profile_pics/') #pip install pillow
21 |
22 | def __str__(self):
23 | return f'{self.user.username} Profile'
24 |
25 | def save(self):
26 | super().save()
27 |
28 | img=Image.open(self.image.path)
29 |
30 | if img.height>300 or img.width>300:
31 | output_size=(300,300)
32 | img.thumbnail(output_size)
33 | img.save(self.image.path)
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/users/views.py:
--------------------------------------------------------------------------------
1 | from django.shortcuts import render, redirect
2 | from django.contrib import messages
3 | from django.contrib.auth.decorators import login_required
4 | from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
5 | from .models import Profile
6 |
7 |
8 | def register(request):
9 | if request.method == 'POST':
10 | form = UserRegisterForm(request.POST)
11 | if form.is_valid():
12 | form.save()
13 | username = form.cleaned_data.get('username')
14 | messages.success(request, f'Account created for {username}!')
15 | return redirect('login')
16 | else:
17 | form = UserRegisterForm()
18 | return render(request, 'users/register.html', {'form': form})
19 |
20 |
21 | @login_required
22 | def profile(request):
23 | if request.method == 'POST':
24 | u_form = UserUpdateForm(request.POST,instance=request.user)
25 | p_form = ProfileUpdateForm(request.POST,request.FILES,instance=request.user.profile)
26 |
27 | if u_form.is_valid() and p_form.is_valid():
28 | u_form.save()
29 | p_form.save()
30 | messages.success(request, f'Your Account has been updated')
31 | return redirect('profile')
32 | else:
33 | u_form = UserUpdateForm(instance=request.user)
34 | p_form = ProfileUpdateForm(instance=request.user.profile)
35 |
36 |
37 | context = {
38 | 'u_form': u_form,
39 | 'p_form': p_form
40 | }
41 | return render(request, 'users/profile.html', context)
42 |
43 |
--------------------------------------------------------------------------------
/static/blog/main.css:
--------------------------------------------------------------------------------
1 | body {
2 | background: #fafafa;
3 | color: #333333;
4 | margin-top: 5rem;
5 | }
6 |
7 | h1,
8 | h2,
9 | h3,
10 | h4,
11 | h5,
12 | h6 {
13 | color: #444444;
14 | }
15 |
16 | ul {
17 | margin: 0;
18 | }
19 |
20 | .bg-steel {
21 | background-color: #5f788a;
22 | }
23 |
24 | .site-header .navbar-nav .nav-link {
25 | color: #cbd5db;
26 | }
27 |
28 | .site-header .navbar-nav .nav-link:hover {
29 | color: #ffffff;
30 | }
31 |
32 | .site-header .navbar-nav .nav-link.active {
33 | font-weight: 500;
34 | }
35 |
36 | .content-section {
37 | background: #ffffff;
38 | padding: 10px 20px;
39 | border: 1px solid #dddddd;
40 | border-radius: 3px;
41 | margin-bottom: 20px;
42 | }
43 |
44 | .article-title {
45 | color: #444444;
46 | }
47 |
48 | a.article-title:hover {
49 | color: #428bca;
50 | text-decoration: none;
51 | }
52 |
53 | .article-content {
54 | white-space: pre-line;
55 | }
56 |
57 | .article-img {
58 | height: 65px;
59 | width: 65px;
60 | margin-right: 16px;
61 | }
62 |
63 | .article-metadata {
64 | padding-bottom: 1px;
65 | margin-bottom: 4px;
66 | border-bottom: 1px solid #e3e3e3
67 | }
68 |
69 | .article-metadata a:hover {
70 | color: #333;
71 | text-decoration: none;
72 | }
73 |
74 | .article-svg {
75 | width: 25px;
76 | height: 25px;
77 | vertical-align: middle;
78 | }
79 |
80 | .account-img {
81 | height: 125px;
82 | width: 125px;
83 | margin-right: 20px;
84 | margin-bottom: 16px;
85 | }
86 |
87 | .account-heading {
88 | font-size: 2.5rem;
89 | }
--------------------------------------------------------------------------------
/blog/views.py:
--------------------------------------------------------------------------------
1 | from django.shortcuts import render
2 | from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
3 | from django.views.generic import (
4 | ListView,
5 | DetailView,
6 | CreateView,
7 | UpdateView,
8 | DeleteView
9 | )
10 | from .models import Post
11 | from django.core.paginator import Paginator
12 |
13 | def home_view(request):
14 | context = {
15 | 'posts': Post.objects.all()
16 | }
17 | return render(request, 'blog/home.html', context)
18 |
19 |
20 | class PostListView(ListView):
21 | model = Post
22 | template_name = 'blog/home.html'
23 | context_object_name = 'posts'
24 | ordering = ['-date_posted']
25 | paginate_by = 2
26 |
27 |
28 | class PostDetailView(DetailView):
29 | model = Post
30 |
31 |
32 | class PostCreateView(LoginRequiredMixin, CreateView):
33 | model = Post
34 | fields = ['title', 'content']
35 |
36 | def form_valid(self, form):
37 | form.instance.author = self.request.user
38 | return super().form_valid(form)
39 |
40 |
41 |
42 | class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
43 | model = Post
44 | fields = ['title', 'content']
45 |
46 | def form_valid(self, form):
47 | form.instance.author = self.request.user
48 | return super().form_valid(form)
49 |
50 | def test_func(self):
51 | post = self.get_object()
52 | if self.request.user == post.author:
53 | return True
54 | return False
55 |
56 |
57 | class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
58 | model = Post
59 | success_url = '/'
60 |
61 | def test_func(self):
62 | post = self.get_object()
63 | if self.request.user == post.author:
64 | return True
65 | return False
66 |
67 |
68 | def about(request):
69 | return render(request, 'blog/about.html', {'title': 'About'})
--------------------------------------------------------------------------------
/users/templates/users/base.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | MyDjangoApp
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
37 |
38 |
39 |
40 |
41 | {% block content %}{% endblock %}
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
Developed by: AJ
50 |
51 |
52 |
Copyright © Arjun 2021
53 |
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/templates/blog/base.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | {% load static %}
10 | {% if title %}
11 | Django Blog - {{ title }}
12 | {% else %}
13 | Django Blog
14 | {% endif %}
15 |
16 |
17 |
18 |
43 |
44 |
45 |
46 | {% if messages %} {% for message in messages %}
47 |
48 | {{ message }}
49 |
50 | {% endfor %} {% endif %}
51 |
52 |
64 |
65 |
66 |
67 |
68 |
69 | Latest Posts
70 | Old Post
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/trydjango/settings.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for trydjango project.
3 |
4 | Generated by 'django-admin startproject' using Django 3.2.4.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/3.2/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/3.2/ref/settings/
11 | """
12 |
13 | import os
14 | from pathlib import Path
15 |
16 | # Build paths inside the project like this: BASE_DIR / 'subdir'.
17 | BASE_DIR = Path(__file__).resolve().parent.parent
18 | #BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
19 | #it tells django where the manage.py is stored
20 |
21 | # Quick-start development settings - unsuitable for production
22 | # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
23 |
24 | # SECURITY WARNING: keep the secret key used in production secret!
25 | SECRET_KEY = 'django-insecure-2zb40@1dl+k8$-2h+ua392#@e)!zn*kdxisb!ygrsgjb8ycywrj'
26 |
27 | # SECURITY WARNING: don't run with debug turned on in production!
28 | #When your project is LiveProject or is on LiveServer set to False
29 | DEBUG = True
30 |
31 | ALLOWED_HOSTS = []
32 |
33 |
34 | # Application definition
35 |
36 | INSTALLED_APPS = [
37 | 'django.contrib.admin',
38 | 'django.contrib.auth',
39 | 'django.contrib.contenttypes',
40 | 'django.contrib.sessions',
41 | 'django.contrib.messages',
42 | 'django.contrib.staticfiles',
43 | 'crispy_forms', #an application that helps to manage Django forms. It allows adjusting form properties (such as method, send button or CSS classes) on the backend without having to re-write them in the template.
44 |
45 | #own app
46 | 'users',
47 | 'blog',
48 | ]
49 |
50 | #This represents how req and security is handled
51 | MIDDLEWARE = [
52 | 'django.middleware.security.SecurityMiddleware',
53 | 'django.contrib.sessions.middleware.SessionMiddleware',
54 | 'django.middleware.common.CommonMiddleware',
55 | 'django.middleware.csrf.CsrfViewMiddleware',
56 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
57 | 'django.contrib.messages.middleware.MessageMiddleware',
58 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
59 | ]
60 |
61 | #this tells Django how to route any url
62 | ROOT_URLCONF = 'trydjango.urls'
63 |
64 | #Django renders HTML contents from here
65 | TEMPLATES = [
66 | {
67 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
68 | 'DIRS': [BASE_DIR / 'templates'],
69 | 'APP_DIRS': True,
70 | 'OPTIONS': {
71 | 'context_processors': [
72 | 'django.template.context_processors.debug',
73 | 'django.template.context_processors.request',
74 | 'django.contrib.auth.context_processors.auth',
75 | 'django.contrib.messages.context_processors.messages',
76 | ],
77 | },
78 | },
79 | ]
80 |
81 | WSGI_APPLICATION = 'trydjango.wsgi.application'
82 |
83 |
84 | # Database
85 | # https://docs.djangoproject.com/en/3.2/ref/settings/#databases
86 |
87 | DATABASES = {
88 | 'default': {
89 | 'ENGINE': 'django.db.backends.sqlite3',
90 | 'NAME': BASE_DIR / 'db.sqlite3',
91 | }
92 | }
93 |
94 |
95 | # Password validation
96 | # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
97 |
98 | AUTH_PASSWORD_VALIDATORS = [
99 | {
100 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
101 | },
102 | {
103 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
104 | },
105 | {
106 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
107 | },
108 | {
109 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
110 | },
111 | ]
112 |
113 |
114 | # Internationalization
115 | # https://docs.djangoproject.com/en/3.2/topics/i18n/
116 |
117 | LANGUAGE_CODE = 'en-us'
118 |
119 | TIME_ZONE = 'UTC'
120 |
121 | USE_I18N = True
122 |
123 | USE_L10N = True
124 |
125 | USE_TZ = True
126 |
127 |
128 | # Static files stores (CSS, JavaScript, Images)
129 | # https://docs.djangoproject.com/en/3.2/howto/static-files/
130 |
131 | STATIC_URL = '/static/'
132 |
133 | CRISPY_TEMPLATE_PACK = 'bootstrap4'
134 |
135 | LOGIN_REDIRECT_URL = 'blog-home'
136 | LOGIN_URL = 'login'
137 |
138 | # Default primary key field type
139 | # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
140 |
141 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
142 |
143 | MEDIA_URL = '/media/'
144 | MEDIA_ROOT = os.path.join(BASE_DIR,'media')
145 |
146 |
147 | EMAIL_BACKEND= 'django.core.mail.backends.smtp.EmailBackend'
148 | EMAIL_USE_SSL = True
149 | EMAIL_HOST = 'smtp.gmail.com'
150 | EMAIL_HOST_USER = 'arjunsdhav@gmail.com'
151 | EMAIL_HOST_PASSWORD = '**********'
152 | EMAIL_PORT = '465'
153 |
--------------------------------------------------------------------------------
/users/templates/users/home.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | Home|Django
11 | {% load static %}
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
45 |
46 |
47 |
48 |
49 | {% block content %}{% endblock %}
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
Thank You! for visiting my blog site.
58 |
59 |
60 |
61 |
62 |
63 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/users/static/css/home.css:
--------------------------------------------------------------------------------
1 | .header-user-dropdown {
2 | background-color: #292c2f;
3 | box-shadow: 0 1px 1px #ccc;
4 | padding: 20px 40px;
5 | height: 80px;
6 | color: #ffffff;
7 | box-sizing: border-box;
8 | overflow: visible;
9 | }
10 |
11 | .header-user-dropdown .header-limiter {
12 | max-width: 1200px;
13 | text-align: center;
14 | margin: 0 auto;
15 | }
16 |
17 |
18 | /* Logo */
19 |
20 | .header-user-dropdown .header-limiter h1 {
21 | float: left;
22 | font: normal 28px Cookie, Arial, Helvetica, sans-serif;
23 | line-height: 40px;
24 | margin: 0;
25 | }
26 |
27 | .header-user-dropdown .header-limiter h1 span {
28 | color: #bc40df;
29 | }
30 |
31 |
32 | /* The header links */
33 |
34 | ul#menu li {
35 | display: inline;
36 | }
37 |
38 | .header-user-dropdown .header-limiter a {
39 | color: #ffffff;
40 | text-decoration: none;
41 | }
42 |
43 | .header-user-dropdown .header-limiter nav {
44 | font: 16px Arial, Helvetica, sans-serif;
45 | line-height: 40px;
46 | float: left;
47 | margin: 0 0 0 60px;
48 | padding: 0;
49 | }
50 |
51 | .header-user-dropdown .header-limiter nav a {
52 | font-size: 14px;
53 | display: inline-block;
54 | padding: 0 12px;
55 | opacity: 0.9;
56 | position: relative;
57 | text-decoration: none;
58 | line-height: 1;
59 | }
60 |
61 | .header-user-dropdown .header-limiter nav a:hover {
62 | opacity: 1;
63 | }
64 |
65 |
66 | /* The "new" badge */
67 |
68 | .header-user-dropdown .header-limiter nav a span {
69 | position: absolute;
70 | top: -12px;
71 | border-radius: 2px;
72 | background-color: #2A6A92;
73 | box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.12);
74 | height: 13px;
75 | font-size: 11px;
76 | right: -15px;
77 | padding: 1px 4px 0;
78 | }
79 |
80 |
81 | /* The user menu */
82 |
83 | .header-user-dropdown .header-limiter .header-user-menu {
84 | position: relative;
85 | float: right;
86 | background-color: #3a3c3e;
87 | width: 75px;
88 | height: 40px;
89 | border-radius: 2px;
90 | }
91 |
92 | .header-user-dropdown .header-limiter .header-user-menu:before {
93 | content: '';
94 | width: 0;
95 | height: 0;
96 | top: 18px;
97 | left: 15px;
98 | position: absolute;
99 | border: 5px solid transparent;
100 | border-top-color: #fff;
101 | }
102 |
103 | .header-user-dropdown .header-limiter .header-user-menu ul {
104 | display: none;
105 | font: bold 13px Arial, Helvetica, sans-serif;
106 | background-color: inherit;
107 | list-style: none;
108 | position: absolute;
109 | text-align: center;
110 | width: 125px;
111 | top: 25px;
112 | right: 0;
113 | padding: 10px;
114 | border-radius: 2px;
115 | box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1);
116 | box-sizing: border-box;
117 | }
118 |
119 | .header-user-dropdown .header-user-menu:active ul,
120 | .header-user-dropdown .header-user-menu:hover ul,
121 | .header-user-dropdown .header-user-menu.show ul {
122 | display: block;
123 | }
124 |
125 | .header-user-dropdown .header-user-menu ul li {
126 | padding: 5px;
127 | }
128 |
129 | .header-user-dropdown .header-user-menu ul li a:hover {
130 | opacity: 0.9;
131 | }
132 |
133 | .header-user-dropdown .header-user-menu ul li a.highlight {
134 | color: #e9ac09;
135 | }
136 |
137 | .header-user-dropdown .header-user-menu img {
138 | border-radius: 50%;
139 | position: absolute;
140 | top: 6px;
141 | max-width: 28px;
142 | right: 10px;
143 | }
144 |
145 | @media all and (max-width: 800px) {
146 | .header-user-dropdown .header-limiter h1 {
147 | font-size: 24px;
148 | }
149 | .header-user-dropdown .header-limiter nav a {
150 | padding: 0 5px;
151 | }
152 | .header-user-dropdown .header-limiter nav {
153 | margin-left: 20px;
154 | }
155 | }
156 |
157 | @media all and (max-width: 600px) {
158 | .header-user-dropdown .header-limiter nav {
159 | display: none;
160 | }
161 | }
162 |
163 |
164 | /* footer*/
165 |
166 | .footer-dark {
167 | padding: 50px 0;
168 | color: #f0f9ff;
169 | background-color: #282d32;
170 | }
171 |
172 | .footer-dark h3 {
173 | margin-top: 0;
174 | margin-bottom: 12px;
175 | font-weight: bold;
176 | font-size: 16px;
177 | }
178 |
179 | .footer-dark ul {
180 | padding: 0;
181 | list-style: none;
182 | line-height: 1.6;
183 | font-size: 14px;
184 | margin-bottom: 0;
185 | }
186 |
187 | .footer-dark ul a {
188 | color: inherit;
189 | text-decoration: none;
190 | opacity: 0.6;
191 | }
192 |
193 | .footer-dark ul a:hover {
194 | opacity: 0.8;
195 | }
196 |
197 | @media (max-width:767px) {
198 | .footer-dark .item:not(.social) {
199 | text-align: center;
200 | padding-bottom: 20px;
201 | }
202 | }
203 |
204 | .footer-dark .item.text {
205 | margin-bottom: 36px;
206 | }
207 |
208 | @media (max-width:767px) {
209 | .footer-dark .item.text {
210 | margin-bottom: 0;
211 | }
212 | }
213 |
214 | .footer-dark .item.text p {
215 | opacity: 0.6;
216 | margin-bottom: 0;
217 | }
218 |
219 | .footer-dark .item.social {
220 | text-align: center;
221 | }
222 |
223 | @media (max-width:991px) {
224 | .footer-dark .item.social {
225 | text-align: center;
226 | margin-top: 20px;
227 | }
228 | }
229 |
230 | .footer-dark .item.social>a {
231 | font-size: 20px;
232 | width: 36px;
233 | height: 36px;
234 | line-height: 36px;
235 | display: inline-block;
236 | text-align: center;
237 | border-radius: 50%;
238 | box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.4);
239 | margin: 0 8px;
240 | color: #fff;
241 | opacity: 0.75;
242 | }
243 |
244 | .footer-dark .item.social>a:hover {
245 | opacity: 0.9;
246 | }
247 |
248 | .footer-dark .copyright {
249 | text-align: center;
250 | padding-top: 24px;
251 | opacity: 0.3;
252 | font-size: 13px;
253 | margin-bottom: 0;
254 | }
--------------------------------------------------------------------------------
/Django_Tut.txt:
--------------------------------------------------------------------------------
1 | Django:
2 | DJANGO uses MVT Architecture i.e
3 | i)Model: The goal of the Models is to handle the data(realated to DBMS)
4 | Each model class maps to a single table in the database.
5 |
6 | ii)View: First, it is responsible for getting the inputs that come via HTTP request.
7 | Secondly,it is a place where we put our business logic of the application.
8 | It's a python function which is used to perform some business logic and return a response to the user.
9 | This response can be the HTML contents of a Web page, or a redirect, or a 404 error.
10 |
11 | iii)Template: Django provides a convenient way to generate dynamic HTML pages by using its template system.
12 | A template consists of static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.
13 |
14 |
15 | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
16 | Step 1: Creating Project(Demo),
17 | >> mkvirtualenv my_django_environment //making virtualenv
18 |
19 | >>django-admin startproject projectname //creating projectfile
20 |
21 | >>python manage.py startapp products //creates an app
22 |
23 | >>python manage.py migrate //to sync settings
24 |
25 | >>python manage.py createsuperuser //creates admin user
26 |
27 | >>Name
28 | >>email
29 | >>pass:
30 | >>cpass:
31 | >>y
32 |
33 | >>python manage.py runserver
34 | 127.0.0.1:8000/admin /*You will get dashboard of admin*/
35 |
36 | Step 2: In settings.py add 'products' app in Installed aPPS AND
37 | Manually Create urls.py in Apps Root(products)
38 |
39 | Step 3: Next step is to create DB models,so we will do these in models.py
40 | Here :
41 | class Product(models.Model):
42 | title = models.CharField(max_length=120) #models.Field is used to map these to database
43 | description =models.TextField(blank=True,null=True)
44 | price =models.DecimalField(decimal_places=2,max_digits=500)
45 | summary=models.TextField()
46 |
47 | IMP: For every change in models.py,don't forget to make migrations and then migrate it
48 |
49 | >>python manage.py makemigrations
50 |
51 | >>python manage.py migrate
52 |
53 | Step 4: Next step is to strictly register models in admin.py
54 | Here:
55 | from .models import Product
56 |
57 | admin.site.register(Product)
58 |
59 | then run server and checker whether the model is created in admin app.
60 |
61 | Step5: Now if we want to create product object in Python Shell(Its a Django API to interact with Database)
62 | >>python manage.py python shell
63 |
64 | So now we are in Python Shell:
65 | >>>from products.models import Product //this is done to import all models of App
66 | >>>Products.objects.all() //shows list of objects created
67 |
68 | //To create the object follow bellow command
69 | >>>p=Product.objects.create(title='Product1', description='New Product', price='200',summary='Thats Sweet')
70 | >>>q.save() //to save the change in DB
71 | >>>q.id //gives id of object
72 |
73 | >>>Product.objects.create(title='Product2', description='New Product2', price='100',summary='Thats Sweet')
74 | >>>Product.objects.create(title='Product3', description='New Product3', price='300',summary='Thats Sweet')
75 |
76 | //So to exit from shell just cmommand
77 | >>>exit()
78 |
79 | >>python manage.py runserver //and check whether objects are created in admin app
80 |
81 |
82 | -----------------------------------------------------------------------------------------------------------------------------------------
83 |
84 |
85 | ****************** To change Default HomePage(http://127.0.0.1:8000/ ) to Custom Homepage **********************************
86 |
87 | Step1: For that create a new app Pages and then open views.py of Pages App
88 |
89 | Note: views.py is place that handles various webpages this done using python fuctions or classes.
90 | and dont foget to import :
91 | eg:
92 | from django.http import HttpResponse
93 | def home_view(request,*args,**args):
94 | return HttpResponse("Hello World ") //Html String
95 |
96 | Step2: Next step is to map these with Project urls.py and import views
97 |
98 | /*from pages import views
99 | from products import views */
100 | //Its bit confuse
101 | OR
102 | /* from pages.views import home_view //Just import a view we want use
103 | urlpatterns=[
104 | path('',home_view,name='home'),
105 | path('admin/',admin.site.urls),
106 | ]
107 | */
108 | Now Runserver, we will get desired Custom Page at http://127.0.0.1:8000/
109 |
110 | And we if we do
111 | /*
112 | urlpatterns=[
113 | path('home/',home_view,name='home'),
114 | path('admin/',admin.site.urls),
115 | ]
116 | */
117 | then we get custome page at http://127.0.0.1:8000/home/
118 |
119 | ----------------------------------------------------------------------------------------
120 |
121 |
122 | ************************ Templates and Django ******************************************
123 | #Create templates folder in Project Root folder and create home.html file
124 | and then in settings.py in templates
125 | TEMPLATES = [
126 | {
127 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
128 | 'DIRS': [BASE_DIR / 'templates'], #add BASE DIR path along with templates
129 | 'APP_DIRS': True,
130 | 'OPTIONS': {
131 | 'context_processors': [
132 | 'django.template.context_processors.debug',
133 | 'django.template.context_processors.request',
134 | 'django.contrib.auth.context_processors.auth',
135 | 'django.contrib.messages.context_processors.messages',
136 | ],
137 | },
138 | },
139 | ]
140 |
141 |
142 |
143 | from django.shortcuts import render
144 | from django.http import HttpResponse
145 |
146 | def home_view(request,*args,**args):
147 | return render(request,"home.html",{})
148 |
149 | --------------------------------------------------------------------------------------
150 |
151 |
152 | ************************** Django Templating Engine ****************************************
153 | templates/home.html
154 |
155 | Hello World
156 | {{ request.user }} //This is Jinja Template Syntax
157 | You are in Home
158 |
159 | Now add index.html in Project root that holds all the html files
160 |
161 | index.html:
162 |
163 |
164 |
165 | Try Django
166 |
167 |
168 | This is Base Index Page
169 | {% block content %}
170 | Its Index Page
171 | {% endblock %}
172 |
173 |
174 |
175 | home.html:
176 | {% extends 'index.html' %} //This relates(inherites) index.html and home.html
177 | {% block content %}
178 | Hello World
179 |
180 | You are in Contact Page
181 | {% endblock %}
182 |
183 |
184 | -------------------------------------------------------------------------------------------------
185 |
186 |
187 | **************Include Template Tag****************
188 | #Create a navbar.html:
189 |
190 |
191 | Home
192 | Contact
193 | About us
194 |
195 |
196 | index.html:
197 |
198 |
199 |
200 |
201 | Try Django
202 |
203 |
204 |
205 | {% include 'navbar.html'%}
206 |
207 | {% block content %}
208 | Its Index Page
209 | {% endblock %}
210 |
211 |
212 |
213 |
214 | All the content is navbar is reflected where ever the index is inherited
215 |
216 |
217 | ----------------------------------------------------------------------------------------
218 |
219 |
220 | ********** Rendering Context in Template ***********
221 | Django takes our template & template context,mashes then
222 | it renders and sends raw HTML to browser
223 |
224 | {{ }} it represents context variable
225 |
226 |
227 | In views.py:
228 |
229 | def about_view(request,*args, **kargs):
230 | context={
231 | "text":"This is About us",
232 | "id":7020
233 | }
234 | return render(request,'about.html',context)
235 |
236 |
237 | In about.html:
238 | {% extends 'index.html' %}
239 | {% block content %}
240 | Hello World
241 | You are in About us Page
242 |
243 |
244 | {{ text }}, {{ id }}
245 |
246 | {% endblock %}
247 |
248 |
249 | O/P:
250 | Hello World
251 |
252 | You are in About us Page
253 |
254 | This is About us, 7020
255 |
256 | --------------------------------------------------------------------------------------------
257 |
258 |
259 | **********For Loop in Template ***********
260 | What if we want to view a list
261 |
262 | def about_view(request,*args, **kargs):
263 | context={
264 | "list"=[123,456,789]
265 | }
266 | return render(request,'about.html',context)
267 |
268 | and in html:
269 | {{ list }}
270 |
271 | O/P: [123,456,789]
272 |
273 | But we want show this as html list items,so we can use forloop in template as below:
274 | about.html:
275 |
276 | {% extends 'index.html' %}
277 | {% block content %}
278 | Hello World
279 | You are in About us Page
280 |
281 |
282 | {{ text }}, {{ id }}
283 |
284 |
285 |
286 | {% for item in list %}
287 | {{ forloop.counter }} - {{ item }}
288 | {% endfor %}
289 |
290 |
291 | {% endblock %}
292 |
293 |
294 | ------------------------------------------------------------------------------------------------
295 |
296 |
297 | *********** Using Conditions in Template************
298 |
299 | {% for abc in list %}
300 | {% if abc == 123 %}
301 | {{ forloop.counter }} - {{ abc|add:22 }}
302 | {% elif abc == "Arjun" %}
303 | Myself Arjun
304 | {% else %}
305 | {{ forloop.counter }} - {{ abc }}
306 | {% endif %}
307 | {% endfor %}
308 |
309 |
310 | -----------------------------------------------------------------------
311 |
312 |
313 | ******************** Template Tags and Filters ****************************
314 |
315 | Do visit: https://docs.djangoproject.com/en/3.2/ref/templates/builtins/
316 |
317 | Ex:
318 |
319 | Open views.py:
320 |
321 | def about_view(request,*args, **kargs):
322 | context={
323 | "title":"This is About us",
324 | "id":7020,
325 | "list":[123,"Kunal","Arjun",264]
326 | "html":" Hello HTML "
327 | }
328 | return render(request,'about.html',context)
329 |
330 |
331 | Open About.html and pass:
332 |
333 | {{ html }} //it gives the string as it is
334 |
335 | but if we use filter: {{ html|safe}} //it gives O/P as HTML string
336 |
337 |
338 | -------------------------------------------------------------------------------------
339 |
340 |
341 |
342 | *********** Render Data From Database with a Model *******************************
343 | Run: python manage.py shell
344 |
345 | Now,we are in shell:
346 |
347 | >>>from products.models import Product //first step is to import models from app
348 |
349 | >>>Product.objects.get(id=1) //will give object with id=1
350 | //to get exact object use __str__ function in models
351 |
352 | //also we can store command in variable so that we can use them for our ease
353 | >>>obj=Product.objects.set('title')
354 | >>>obj.id
355 | >>>obj.save()
356 |
357 | >>>exit() //we will exit from shell
358 |
359 |
360 | ---------------------------------------------------------------------------------------------------
361 |
362 |
363 | ****************** Django Model Forms ***************
364 | step1:Create a file "forms.py" in your app folder
365 |
366 | Step2:open forms.py:
367 |
368 | from django import form
369 | from .models import Product
370 |
371 | class Productform(forms.ModelForm):
372 | classMeta:
373 | model=Products
374 | fields=[
375 | 'title',
376 | 'description',
377 | 'price'
378 | ]
379 | step3:Now open views.py and import:
380 |
381 | from .forms import ProductForm
382 |
383 | def product_create_view(request): //to get form data
384 | form=Productform(request.POST or None)
385 | if form.is_valid():
386 | form.save()
387 | form=Productform() //it will reset form field as we click submit
388 | context={
389 | 'form':form
390 | }
391 |
392 | return render(request,"product/product_create.html")
393 |
394 | Step4: Then create 'product_create.html' to store html of form:
395 | open product_create.html:
396 | {% extends 'index.html' %}
397 | {% block content %}
398 |
403 | {% endblock %}
404 |
405 | Step5:Give its path to projects urls.py:
406 | import product_create_view
407 |
408 | and then in url patterns=[ path('create',product_create_view), ]
409 |
410 |
411 | --------------------------------------------------------------------------------------------------
412 |
413 |
414 | ************************** Pure Django Form ***************************
415 | step1: For this we have to create a sample of form by creating class in forms.py
416 |
417 | class RawProductForm(forms.Form):
418 | title =forms.CharField()
419 | description=forms.CharField()
420 | price =forms.DecimalField()
421 |
422 | Do refer: https://docs.djangoproject.com/en/3.2/ref/forms/fields/
423 |
424 | step2:Open views.py:
425 | from .forms import RawProductForm
426 |
427 | def product_create_view(request.POST):
428 | my_form=RawProductForm()
429 | context = {
430 | "form": my_form
431 | }
432 |
433 | return render(request, "product/product_create.html",context)
434 |
435 | step3:Open product_create.html:
436 |
437 | {% extends 'index.html' %}
438 | {% block content %}
439 |
444 |
445 | {% endblock %}
446 |
447 |
448 | ------------------------------------------------------------------------------------------------------------------------------------------
449 |
450 | ****************************** Form Widgets ************************************************
451 | This changes will give intial values to fields:
452 | forms.py:
453 |
454 | class RawProductForm(forms.Form):
455 | title =forms.CharField( label='Product Name', widget=forms.TextInput(attrs={"placeholder":"Enter Product"}))
456 | description=forms.CharField(required=False,
457 | widget=forms.Textarea(
458 | attrs={
459 | "placeholder":"Enter Description",
460 | "class":"my_class",
461 | "id" :"my_id",
462 | "rows":20,
463 | "cols":120
464 | }
465 | )
466 | )
467 | price =forms.DecimalField(initial=199.99)
468 |
469 |
470 | ----------------------------------------------------------------------------------------------------------------------------
471 |
472 |
473 | ************************** Form Vailidation ******************************************************************************
474 |
475 | Suppose we want to add validation for Title then we will add new function cleaned_ in our real Product form.
476 | forms.py:
477 |
478 | class ProductForm(forms.ModelForm):
479 | title = forms.CharField(label='Product Name', widget=forms.TextInput(
480 | attrs={"placeholder": "Enter Product"}))
481 |
482 | description = forms.CharField(required=False,
483 | widget=forms.Textarea(
484 | attrs={
485 | "placeholder": "Enter Description",
486 | "class": "my_class",
487 | "id": "my_id",
488 | "rows": 10,
489 | "cols": 50
490 | }
491 | )
492 | )
493 | price = forms.DecimalField(initial=99.99)
494 |
495 | class Meta:
496 | model = Product
497 | fields = [
498 | 'title',
499 | 'description',
500 | 'price'
501 | ]
502 | //validation for title
503 | def clean_title(self, *args, **kargs):
504 | title=self.cleaned_data.get("title")
505 | if not title.startswith("Product"):
506 | raise forms.ValidationError("Enter Valid Product")
507 | return title
508 |
509 |
510 |
511 | --------------------------------------------------------------------------------------------------
512 |
513 |
514 | ********************* Dynamic URL Routing **********************************************************************
515 |
516 |
517 | step1: Create view in views.py:
518 |
519 | def dynamic_lookup_view(request, my_id):
520 | obj = Product.objects.get(id=my_id)
521 | context = {
522 | "object": obj
523 | }
524 | return render(request, "product/detail.html", context)
525 |
526 | step2: Include path of view to urls.py of project dir
527 |
528 | urls.py:
529 | from products.views import dynamic_lookup_view
530 |
531 | urlpatterns=[
532 |
533 | path('products/',dynamic_lookup_view)
534 | ]
535 |
536 |
537 | and try: http://127.0.0.1:8000/products/1/ should give object1
538 | http://127.0.0.1:8000/products/2/ should give object2
539 |
540 |
541 | -----------------------------------------------------------------------------------------------------------------------------
542 |
543 |
544 | ************************* Handle DoesNotExist Error *******************************
545 | step1: if we tried to access object that does not exist,it will give error as
546 | "DoesNotExist at/products/14/" this error is not so standard.
547 |
548 | step2: from django.shortcuts import get_object_or_404 //for handling does not exist error
549 |
550 | def dynamic_lookup_view(request, my_id):
551 | object= get_object_or_404(Product, id=my_id)
552 | context = {
553 | "obj": object
554 | }
555 | return render(request, "product/detail.html", context)
556 |
557 | After making this changes,if we tried to access object that does not exist,it will give error as
558 | "Page not Found" which is standard error message
559 |
560 |
561 | ------------------------------------------------------------------------------------------------------------------------------
562 |
563 |
564 | *************************** View of a List of DB objects ***********************************************
565 | Step1:
566 |
567 | Lets create a view in views.py:
568 |
569 | def product_list_view(request):
570 | queryset = Product.objects.all() #list of objects
571 | context = {
572 | 'object_list':queryset
573 | }
574 | return render(request, 'product/product_list.html', context)
575 |
576 | Step 2: Create product_list.html:
577 |
578 | {% block content %}
579 | {% for i in object_list %}
580 |
581 | {{ i.id }} - {{ i.title }}
582 |
583 | {% endfor%}
584 | {% endblock %}
585 |
586 | --------------------------------------------------------------------------------------------------------------------------
587 |
588 |
589 | **************************** Dynamic Linking URL ************************************
590 |
591 | step1: In models.py of products define a new function in our model Class
592 |
593 | def get_absolute_url(self):
594 | return f'/products/{self.id}'
595 |
596 | step2:
597 | {% for i in object_list %}
598 |
599 | {{ i.id }} - {{ i.title }}
600 |
601 | {% endfor%}
602 |
603 |
604 |
--------------------------------------------------------------------------------