Starting with Django: Adding your App to your Project

Last week we looked at how we create an app and it was relatively simple (without having really looked into any of the specifics), but as it stands, your app is just sitting there and in not accessible. So this week we are going to have a look at how to attach your app properly to the project.

There are a few key things that have to happen at this stage and they are all done within the project.

Add the App to settings.py

As we noted in the article that broke down the project, settings.py holds all the core information about the project itself. One of the key functions here is to contain all of the apps. This can be found under the heading "INSTALLED_APPS". You will see other items already there like django.contrib.admin and django.contrib.auth which are used for authentication and administration.

What you need to do is add in your app name. It will likely be in the form folder.apps.AppName. For me it was blog.apps.BlogConfig and my user class was users.apps.UsersConfig.

This tells the project that when manage.py runserver is started that it should look for those apps as well.

The other thing we need to do is put a path in there, because like any website, that is required.

Add a route to urls.py

This is a really key step, because if you dont, then the project has no idea how to pass traffic to the app.

Its a another pretty simple format, and the base urls.py file that comes with the project will even include some basic instructions for you:

The exact format is based on whether its a function, class other url setup, but in essence it is:
path('route', view/include, name='name')
so lets break it down slightly:

path: Its literally the word path as a function

'route': This is the path of what it should be after the address. So for me it was 'blog', which attaches to my home route of fuzzygwalchmei.herokuapps.com as a /blog extension

view/include: This is the part that forwards it to your app. It might be views.function_name.as_views() or include('app_name.urls'), which ever works for what you are doing

name='name': This is a way to name your view for easier reference later. Check out the documentation or tutorial you are working with.

And that's it, with those few bits of basics, you have technically got a web page and an app (assuming you put in a bit more detail than I did at this stage).

Next week we will start looking into some of the more technical components like urls.py, templates, models.py, forms.py, views.py and .... well anything else that comes to mind or seems useful/interesting

Comments

Popular Posts