0% found this document useful (0 votes)
26 views5 pages

Django A

The document provides instructions for setting up basic Django project with an app containing a homepage, about, services, and contact pages. It describes initializing a Django project, creating an app, setting up URLs, views and templates, enabling admin access, adding a contact form, and connecting it to a database model.

Uploaded by

Deekshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Django A

The document provides instructions for setting up basic Django project with an app containing a homepage, about, services, and contact pages. It describes initializing a Django project, creating an app, setting up URLs, views and templates, enabling admin access, adding a contact form, and connecting it to a database model.

Uploaded by

Deekshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

for vscode terminal shortcuts - Ctrl+Shift+P

03-08 ----------------------

1. in the terminal, pip install django


2. in the terminal, pip install django --upgrade
3. in the terminal, django-admin startproject safura_invo (project_name)
4. in the terminal, python manage.py
5. in the terminal, python manage.py startapp my_app (app_name)
6. make a urls.py file in my_app folder
7. copy paste all the code from project-urls.py to app-urls.py
8. in the terminal, python manage.py runserver
9. copy the server link - http://127.0.0.1:8000/
10. paste the link in the browser, install successful
11. in project-urls, paste this --

path('', include('home.urls'))

12. open views.py from my_app folder


13. and paste this function

from django.shortcuts import render, HttpResponse


def index(request):
return HttpResponse('this is home page')

14. then in app-urls.py, add webpages

path("",views.index,name='my_app'),
path("about",views.about,name='about'),
path("services",views.services,name='services'),
path("contact",views.contact,name='contact')

15. then, in views.py create respective functions for pages

def about(request):
return HttpResponse('this is about page')
def services(request):
return HttpResponse('this is services page')
def contact(request):
return HttpResponse('this is contact page')

16. replace home with my_app everywhere


17. in app-urls.py import views

from my_app import views

18. in project_urls, import include

from django.urls import include

19. save everything, copy the link from the terminal and paste in the browser

http://127.0.0.1:8000/ -- loads homepage


http://127.0.0.1:8000/about -- loads about page
http://127.0.0.1:8000/services -- loads services page
http://127.0.0.1:8000/contact -- loads contact page

20. now, create two folders in the parent project folder by the name template and
static
static folder saves images

21. within templates folder, create a html file - index.html


22. add some dummy text in the html file
23. in views.py, under the index function, paste this line instead

return render(request,'index.html')

24. in project-settings.py, under Templates, paste this line,

'DIRS': [BASE_DIR/'templates'],

04-08 -----------------

25. python manage.py makemigrations


26. python manage.py migrate
27. python manage.py createsuperuser
28. creating database admin account,
create superuser admin, leave email address blank, enter password, even when you
type the password, its not visible, its hidden.

Username (leave blank to use 'user'): admin


Email address:
Password:
Password (again):

29. if the admin page does not load, in settings.py under installed_apps, add your
app name at the end

'my_app',

30. to change password, in the terminal, python manage.py changepassword admin


(username)
31. create new superuser the same way as mentioned above
32. now the page will load - http://127.0.0.1:8000/admin/
33. enter the credentials and login
34. template inheritance --------------
35. create a file 'base.html' in templates folder
36. copy + cut + paste the code from index.html
37. now in the terminal, python manage.py runserver
38. to inherit the navbar template in all pages, paste these lines in base.html

all tags need to have this structure:

{% tag_name parameter %}

paste this in the head,

<title>{% block title %}{% endblock title %} |AsmaMentoring </title>

paste this in the body, after nav bar

{% block body %} {% endblock body %}

39. in home.html, extend (inherit) template by pasting these lines

{% extends 'base.html' %}
{% block title %} Home {% endblock title %}
40. paste these lines at the very top and remove the html, head and body tags only
leaving the actual code (code after body)
41. in the place of body open and close tag, paste this

{% block body %} -- (<body>)


{% endblock body %} -- (</body>)

42. as we want the navbar in all of the pages, we have created a base.html file and
pasted the navbar code which is common fo all pages.
and we are passing this template in all the webpages and optimizing the code.
43. create a contacts.html file and paste this at the beginning of the file

{% extends 'base.html' %}
{% block title %} Contact {% endblock title %}

44. then open the body tag like this -

{% block body %} -- (<body>)

enclose all the html code within this tag i.e., forms, buttons, divs etc

close the body tag like this -

{% endblock body %} -- (</body>)

45. we now have two pages up and running - home and contact page.
we can now navigate to these pages with the navbar

09-08 -----------------

46. in models.py-my_app, paste this

from django.db import models


class Contact(models.Model):
name = models.CharField(max_length=122)
email = models.CharField(max_length=122)
phone = models.CharField(max_length=122)
desc = models. TextField()
date = models.DateField()

def __str__(self):
return str(self.name)

47. in admin.py-my_app, paste this

from my_app.models import Contact


# Register your models here.
admin.site.register(Contact)

48.in apps.py, copy 'MyAppConfig'


49. in settings.py, under installed apps, paste 'myapp.apps.MyAppConfig' with
trailing comma
50. load admin - http://127.0.0.1:8000/admin/ a module contact has been created.
51. in views.py, paste this

from django.shortcuts import render, HttpResponse


from datetime import datetime
from home.models import Contact
from django.contrib import messages
def contact(request):
if request.method == "POST":
name = request.POST.get('name')
email = request.POST.get('email')
phone = request.POST.get('phone')
desc = request.POST.get('desc')
contact = Contact(name=name, email=email, phone=phone, desc=desc,
date=datetime.today())
contact.save()
messages.success(request, 'Your message has been sent!')
return render(request, 'contact.html')

also, remove any function for contact

52. load contact page by running the server from the terminal
53. in contact.html, paste this line after the contact-form

( <form method="POST" action="/contact">


{% csrf_token %} )

54. in terminal,

python manage.py makemigrations

then,

python manage.py migrate

then,

python manage.py runserver

55. in contact page, enter details in the input fields and check if it reflects in
the database
if it does not reflect, then a form is not created in contact.html that holds all
these values.
contact table is created now, after making migrations

56. assign an id to all the input fields, so it can be accessed by the contact
method defined in views.py

<div class="container mb-3 py-4">


<h1 class="text-center">Contact Us</h1>
<form method="post" action="/contact">
{% csrf_token %}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name"
placeholder="Enter your Name">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email"
placeholder="Enter Your Email">
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="phone" class="form-control" id="phone" name="phone"
placeholder="Enter Your Phone Number">
</div>
<div class="form-group">
<label for="desc">Tell me about what you want to contact me
for...</label>
<textarea class="form-control" id="desc" rows="3"
name="desc"></textarea>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>

</div>

57. now, load contact page and enter details and press the submit button

58. check database, the entered details are reflected in the database.

You might also like