How to Create a Home page in Django



Python


In this article, we show how to create a home page in Django.

So, to create a home page, you would create an app, just like any other home page.

Just like you would create an app for a page such as a blog or a contact page, for instance, to create a home page in Django, you would create an app.

You can name this app anything such as home or home page.

The line of code to do so would be as that shown below.



If you're on a windows PC, you may have to use py instead of python. This is shown in the code below.



So, after you create this home page, you should see this now in your project directory. I named my project directory, mywebsite, so this home page folder is now found in, mywebsite/homepage.

Now that you have created this app, you will need to do several things.

settings.py File

One thing that must be done is now that you have created a new app for your website, you must enter in this app into the list of INSTALLED_APPS in the settings.py file in the project directory.

Since we created an app called homepage, you must enter in 'homepage' in the list of INSTALLED_APPS.

This is shown in the code below.



So, you can see above how 'homepage' as now been added as one of the installed apps.


urls.py File in the Project Directory, mywebsite

The first thing you will need to do is specify the path to the home page, so that django knows where to redirect users to if they are trying to access the home page. This is done by adding the path to the urlpatterns list.

The urls.py file will look something like what is shown below.



So, you can see how we've added the line, url(r'^', include ('homepage.urls')), to the urlpatterns list. This now specifies that if the ip address (in the case of a local server) or the domain name (in the case of a live site) is specified, the user will be redirected to the homepage app.

You see that the statement, include ('homepage.urls'), is present. This statement references the urls.py file in the homepage app that we've created.

If you look at the homepage app that you've created, then you'll notice that there most likely is no urls.py file in there. So we have to create one.

So create a .py file named urls.py in the homepage directory.

In this file, we specify the url to the home page.

This is shown in the code below.



So, just like with the urls.py file in the project directory, mywebsite, there is a list called urlpatterns. Inside of this list, we specify the path to the home page.

The r stands for raw string. The ^ means begins with and the $ means ends. So, if the ip address or domain name simply begins and ends with nothing, then we know that the user is specifying the home page. This would be specified as, ipaddress or domainnname.com. See nothing exists after the ip address or the domain name, meaning it's the home page. This is in contrast to something like the Contact page on the website which would be at, ipaddress/Contact or www.domainname.com/Contact.

We specify view.index as the view that should be shown when the home page is specified in the URL. And we set the name to index.

This is so that when the django page renders the views.py file in the homepage directory, it looks for the function, index, and renders what is in this function.

We create the views.py file next.

views.py file in the homepage directory

Now the last thing we must do is edit the views.py file.

The views.py file is a file that tells how a page should look and rather what it renders.

If you have a template, you can render a template.

However, in our simple example, we will simply print out some HTML.

So, open the view.py file and enter in the following code.



So, we import the HttpResponse. This allows us to output HTML tags.

We then create the function index with the parameter, request.

In this function, we return h1 tags, This is the home page.

So, after all of this is done, then you can now run the local server through the following code below.



Now, open up a web browser and enter in, http://127.0.0.1:8000

You should now see a page, such as that shown below.

Python



And this is how you can build a home page in Django.


Related Resources

How to Show All Tables of a MySQL Database in Python

How to Count the Number of Rows in a MySQL Table in Python



HTML Comment Box is loading comments...