Hello friends we are back with new topic in our Django series. in previous section we have learned how to install django in python, how to create an empty project, how to setup database for our project. Now we are going to start for our new first project i.e Building Social Bookmarking Application using Django. So lets start
Intro.
In this post we are going to learn about views, models, and templates. we will cover following topics first.
1 To create main page : URL and Views
2 To design initial Database system : Models
3 To create a Template for our main page : Templates
And lastly we will put this all together to create user handy page. So let’s start.
Note that you can download the complete project by clicking on download button
Creating Main Page : URL and Views
The very first thing which comes in our mind while seeing at development server welcome page is creating our own welcome page. So we will wriNote our own welcome page in python and will tell Django to call specific python code when visitor visits to our URL and it will show our own welcome message.
Main Page View:
We are going to write our Django main page view by creating our first Django Application inside our project. A view in Django is nothing but a python function that responds to a page request by generating or creating corresponding page. To create Django application inside project, redirect your terminal path into the project folder and execute the following command.
python manage.py startapp bookmarks
After running this command Django will create a folder named bookmarks inside our project folder. The files will be
__init.py__ : This file will tell python that bookmarks is an python package
view.py : This fill contains code for our home page’s view
models.py : This file contains models required for our application
Now we will create our main page view. To do so open view.py file in code editor and enter the following code or you can download the file from download section.
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def main_page(request):
output = '''
<html>
<head><title>%s</title></head>
<body>
<h1>%s</h1><p>%s</p>
</body>
</html>
''' % (
'Django Bookmarks',
'Welcome to Django Bookmarks',
'Where you can store your Bookmarks')
return HttpResponse(output)
This code will take user input as parameter and will show output of page. but before seeing output we need to connect this code to url. after connected this code we can see the output of our page.
Creating the main page.
As you may remember our previous post we have created url.py file while creating this bookmark project. So open this url.py in code editor. this file contains all urls of our application. and change the code into following code .
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('bookmarks/', include('bookmarks.urls')),
path('admin/', admin.site.urls),
]
Now create another file named urls.py in our project folder i.e. in bookmarks/ directory. And enter following code into this file.
from django.urls import path
from . import views
urlpatterns = [
path('',views.main_page, name='main_page'),
]
Now everything is clear we can now test our first view page. Now launch the development server using following code and goto 127.0.0.1:8000/bookmarks to see the page generated by the view.
Note: If you are getting 404 page not found page that is because you are visiting 127.0.0.1 instead of 127.0.0.1:8000/bookmarks so redirect to it. You will see page like below
Congratulation your first Django view is ready and running.
Here our main page looks like simple without CSS. Therefore we will learn to use templates in our upcoming section. which will make it easy to style our page using stylesheet. we will stop here for now. Till now if you face any problem regarding this feel free to comment below. we will try to resolve as soon as possible. Till then
Happy coding…
Thank you…