|
| 1 | + |
| 2 | +# Intoduction to Django |
| 3 | + |
| 4 | +Code with Harry Django Tutorials Notes |
| 5 | + |
| 6 | +## Steps to Setup the Code |
| 7 | + |
| 8 | +- python3 -m venv myenv |
| 9 | +- source venv/bin/activate |
| 10 | +- pip3 install django |
| 11 | +- django-admin startproject Hello |
| 12 | +- python3 manage.py startapp home |
| 13 | +- Basic file Structure of Project |
| 14 | + - Inside Hello -> urls.py |
| 15 | + ``` |
| 16 | + urlpatterns = [ |
| 17 | + path('', include('home.urls')) #add this routes |
| 18 | + ] |
| 19 | + ``` |
| 20 | + - Inside home -> urls.py |
| 21 | + ``` |
| 22 | + urlpatterns = [ |
| 23 | + path("", views.index, name='home'), |
| 24 | + path("about", views.about, name='about'), |
| 25 | + path("services", views.services, name='services'), |
| 26 | + path("contact", views.contact, name='contact'), |
| 27 | + ] |
| 28 | + ``` |
| 29 | + - Inside home -> views.py |
| 30 | + ``` |
| 31 | + from django.shortcuts import render, HttpResponse |
| 32 | + def index(request): |
| 33 | + context = { |
| 34 | + "variable1":"Harry is great", |
| 35 | + "variable2":"Rohan is great" |
| 36 | + } |
| 37 | + return render(request, 'index.html', context) |
| 38 | + ``` |
| 39 | +- python3 manage.py runserver |
| 40 | +
|
| 41 | +
|
| 42 | +## Access static file and template |
| 43 | +
|
| 44 | +- Inside Hello -> settings.py |
| 45 | + - Added below querry manually for static file |
| 46 | + ``` |
| 47 | + STATICFILES_DIRS = [ |
| 48 | + os.path.join(BASE_DIR, "static") |
| 49 | + ] |
| 50 | + ``` |
| 51 | +
|
| 52 | + - Edit the value of DIRS in TEMPLATES |
| 53 | + ``` |
| 54 | + 'DIRS': [os.path.join(BASE_DIR, "templates")] |
| 55 | + ``` |
| 56 | +
|
| 57 | +## Create superuser for authenficiation/admin panel |
| 58 | +
|
| 59 | +~$ python3 manage.py createsuperuser |
| 60 | +
|
| 61 | +Inside Hello -> urls.py, add below lines to change the Admin Portal |
| 62 | +``` |
| 63 | + admin.site.site_header = "Ice Cream Admin" |
| 64 | + admin.site.site_title = " Ice Cream Admin Portal" |
| 65 | + admin.site.index_title = "Welcome to Ice Creams" |
| 66 | +``` |
| 67 | +
|
| 68 | +## Entries in database |
| 69 | +- Do template inheritance and create form page at conatct us route with {% csrf_token %} |
| 70 | +
|
| 71 | +- Inside home -> models.py |
| 72 | +``` |
| 73 | + class Contact(models.Model): |
| 74 | + name = models.CharField(max_length=122) |
| 75 | + email = models.CharField(max_length=122) |
| 76 | + phone = models.CharField(max_length=12) |
| 77 | + desc = models.TextField() |
| 78 | + date = models.DateField() |
| 79 | + |
| 80 | + def __str__(self): |
| 81 | + return self.name |
| 82 | +``` |
| 83 | +- Inside home -> admin.py |
| 84 | + - Register the model |
| 85 | + ``` |
| 86 | + from home.models import Contact |
| 87 | + admin.site.register(Contact) |
| 88 | + ``` |
| 89 | + - Register app name: Copy app name from apps.py (HomeConfig) and add this at INSTALLED_APPS in Hello's settings.py |
| 90 | + ``` |
| 91 | + INSTALLED_APPS = [ |
| 92 | + 'home.apps.HomeConfig', |
| 93 | + ] |
| 94 | + ``` |
| 95 | + Run below command to create database |
| 96 | +``` |
| 97 | + ~$ python3 manage.py makemigrations |
| 98 | + ~$ python3 manage.py migrate |
| 99 | +``` |
0 commit comments