5 minutes read

After all the hard work of making an Internet service, you obviously want to see the result. What does it take to launch a Django webserver? If you already have an application in your project, there are only a few steps left:

  1. Configure the settings.py file;

  2. Launch the server on your local machine;

  3. Fix the errors (if any) with the help of the debug page.

For convenience, let's use the project about Alan Smithee that you must have started in one of the previous topics and perform the mentioned steps on it.

Once you have the code of the project, the first thing to do is tweaking some configs in the settings.py module to make it work. settings.py is a python module created automatically by Django in the project directory: in our case, smithee/settings.py. It stores all the important information on how your service works and behaves through defining variables. For starting our local server, the important thing is to add our app name to the INSTALLED APPS:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'movies',
]

If you forget to include your app, you may see an exception upon starting your server or accessing a page. For example, the server will not be able to find a template because you haven't registered an app where this template is placed.

Django debug mode

Let's face it: bugs happen. If something went wrong, you want to know where in the application it is. What can help you with that is the debug mode. Debug mode is a state of an application when it shows tracebacks and other useful information in your browser when the server fails. You can start the debug mode by adding DEBUG = True in your settings.py module.

Let's imagine a situation: when the application was under construction, one of the developers forgot the {% endfor %} tag in the movies/templates/index.html that was supposed to be there:

<!DOCTYPE html>
<title>Movies</title>

<h1>Films by {{ director }}</h1>

<ul>
{% for movie in movies %}
  <li>{{ movie.year }} - {{ movie.title }}</li>
</ul>

Now, when you try to access the page with the films, you get this instead:

Error during template rendering

As you see, Django shows the file and line where the mistake is triggered. It's not always that easy to spot the place of an error in your code for Django, but you can use other information from the debug page to find it by yourself.

To see another possible mistake in the project we're constructing, let's delete a variable DIRECTOR we've created at settings.py file. As far as it's used in our template and there's none now, we'll catch such error:Attribute Error

What else can you spoil to catch an error? If you have a problem with templates directory—if an app is not in INSTALLED APPS in settings.py, or there's no index.html in templates directory, or there's something wrong with its name in views.py:

TemplateDoesNotExist error

Our 'Alan Smithee' app is actually too simple to demonstrate all functionality of DEBUG mode, but even now it's obvious that it's really helpful and informative for the developer.

Starting the local server

You have an application, and you configured the settings: looks like you're finally ready to start the server! To launch the server on your local machine, you should run the runserver command from your terminal in the project's root directory:

python manage.py runserver

If you see the message "Error: That port is already in use.", it means that the default port 8000 is in use by some other application. In this case, you can choose any other available port and pass it as the last argument to the command:

python manage.py runserver 9090

Also, you may run the same command in terminal of your IDE (for example, if you're building your app with PyCharm) or edit the configuration in it and start the server without printing a command:

Running server from PyCharm

Running server from PyCharm

Congratulations! Django starts the server and now you can access it through the browser. This is your local server, so you will be able to access it by typing 'localhost' or '127.0.0.1'. You should also include the port that you used, so the final address for you to enter in the browser should look like this: http://localhost:<port_you_used> or http://127.0.0.1:<port_you_used>. Now you will see a part of Alan Smithee's filmography!

Conclusion

From now on, you're able to launch a Django server for your project. You've learned some important settings: INSTALLED_APPS where you should add your app for Django to see it, and DEBUG to activate the debug mode and help you locate the errors. In addition to the existing variables, you can add your own so that they can be reachable throughout the project. Having configured the settings file, you should use the runserver command to launch the server with your app.

14 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo