How to Create an Entire Website from a Single App in Django 


               


How to Create an Entire Website from a Single App in Django



Python


In this article, we go over how to create an entire website from a single app in Django.

In Django, to create a webpage, you don't necessarily need multiple apps.

You can create an entire website with just a single app.

So let's create an entire website on this page.

Let's say, we'll have 4 different pages, the home page, the Blog, the Store Page, and the Contact page.

Let's say we create a project called mywebsite and create an App called webpages.

What you have to do is then go to the settings.py file and put this app 'webpages' in the INSTALLED_APPS list.

So, INSTALLED_APPS would look like the following.



We then have to go into the urls.py file in the project root directory and specify the url for the webpages app.

You should put the following line in the urls.py file.



The line, url(r'^', include('webpages.urls')), makes it so that we can put any specify any URL path for any other page that we want to create, such as http://127.0.0.1:8000/Store, http://127.0.0.1:8000/Contact, http://127.0.0.1:8000/Blog, etc.

The next thing we have to do is create a urls.py file in the webpages app and specify all the paths.



So, if a user types in, http://127.0.0.1:8000/, they will be brought to the home page.

If a user types in, http://127.0.0.1:8000/Store, they will be brought to the Store page.

If a user types in, http://127.0.0.1:8000/Blog, they will be brought to the Blog page.

If a user types in, http://127.0.0.1:8000/Contact, they will be brought to the Contact page.

Now, we have to go to the views.py page, so that the views that we want render for each of these urls are rendered.

In the views.py file, we specify the functions that we specified in the urls.py file.



So, in this views.py file, we created all of the functions and returned the code for each of the web pages.

We have the functions, home, blog, store, and contact. Each returns the HTML for Home Page, Blog Page, Store Page, and Contact Page, respectively.

However, the above HTTPResponse function normally wouldn't be used for web page views.

What you would want to do instead is render a template for each of the web pages.

So, the views.py file would be changed to the following shown below, now rendering templates instead of just hardcoded HTML.



So, now instead of hardcoded HTML, we render HTML templates. We then have to create each of these files that represents the home page, blog page, store page, and contact page.

This is definitely the more professional and real-life way you would build a website in Django. You would almost never use hardcoded HTML, but instead render templates files such as HTML files that form the views for the web pages.

And this is how you can build an entire website with a single app in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...