Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Create a Django Project

In this video, you’ll create a Django project using the django admin command. You can use your terminal window, just as you did in the previous lesson.

Start by running the django admin command with startproject, followed by the name of the project, which in this case is portfolio. If you put a period (.) at the end of the command, then you can avoid creating an extra folder.

00:00 In this video, we will create a Django project using the django-admin command. Just to make sure you know this, you can use your terminal window just as you did before and to make all this happen, as you want. I’m going to stop using the terminal like this now, and instead, I’m going to use it inside of PyCharm, down here, but that’s just another terminal window, right? It’s just integrated in PyCharm, which makes it a bit easier for us to see everything that’s going on. All right!

00:30 So, the first thing we want to do, is we’re going to use the django-admin command. I can always autocomplete, use the Tab character so that you know what’s going on. I’m going to use django-admin, which got installed when we installed Django into our virtual environment.

00:48 With django-admin I’m going to say startproject, and generally, you will want to say the name of the project now. Okay, so we’re going to call this one portfolio.

01:02 And here’s a little trick that’s actually very helpful: if I would just run this command like this without anything else, Django is going to create a folder structure that makes a project for the portfolio and then a management app inside of it called portfolio again.

01:15 So, we already have a folder called django-portfolio, which is going to hold our whole project. I don’t want to repeat this structure again, and the trick to avoid doing that is by just adding a little . here.

01:27 This . generally stands for something that happens inside of this current directory. In this case, it’s the same. It just avoids creating an extra folder and instead just makes the management app that we’re going to call portfolio inside of the folder that we created before.

01:46 So, let’s run this. That went pretty quick. Take a look here. After it refreshes, we can now see that inside of this folder, we have a new one that popped up called portfolio, as we named it here.

02:00 This is the management app for our Django project. So, a Django project, as we talked about in Part 1, consists of multiple apps, or at least one. This is the management app, which holds the settings file, and is the first access point for anything that the user types in the URLs, and it also contains the WSGI file.

02:23 So, the management app is a little different than other apps, in that it contains some project-level settings file, like settings.py. That’s it for now.

02:35 We already have a magically Django-created project that’s running, and we can take a look by executing the built-in server using this command.

02:52 We get a bit of information here that we don’t have to worry about right now, but it also tells us that it’s starting the development server at localhost port 8000. This is where Django starts its development server.

03:04 I can take a look at this.

03:08 And, here we are! Django tells us that the install worked successfully and we have a Django app up and running on our localhost.

Avatar image for tsusadivyago

tsusadivyago on Dec. 28, 2019

My default port 8000 was being used, so I tried with

python manage.py runserver 8080
Avatar image for reblark

reblark on Feb. 18, 2020

Hi Martin, As you know, I completed this course once before. I have completed other courses since but remember this one as the best. So, I am going to run through it again. Already, your detailed commentary helped me solve a problem I was having in another course. That was great and I am looking forward to re-doing this course. Cheers.

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 19, 2020

Hi and welcome back for the second round @reblark. :) Glad it’s been useful, and training your skills by doing exercises over again is definitely a great way to learn. Keep up the motivation!

Avatar image for littleblueline

littleblueline on Feb. 20, 2020

Guys I keep getting the following error as soon as i try to run the server:`` Microsoft Windows [Version 10.0.18362.657] (c) 2019 Microsoft Corporation. All rights reserved. PycharmProjects\Portfolio app>python manage.py runserver Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last): File “H:\Coding\lib\threading.py”, line 926, in _bootstrap_inner self.run() File “H:\Coding\lib\threading.py”, line 870, in run self._target(self._args, self._kwargs) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\utils\autoreload.py”, line 53, in wrapper fn(args, kwargs) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\core\management\commands\runserver.py”, line 109 , in inner_run autoreload.raise_last_exception() File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\utils\autoreload.py”, line 76, in raise_last_exc eption raise _exception[1] File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\core\management__init__.py”, line 357, in execu te autoreload.check_errors(django.setup)() File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\utils\autoreload.py”, line 53, in wrapper fn(*args, kwargs) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django__init__.py”, line 24, in setup apps.populate(settings.INSTALLED_APPS) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\apps\registry.py”, line 114, in populate app_config.import_models() File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\apps\config.py”, line 211, in import_models self.models_module = import_module(models_module_name) File “H:\Coding\lib\importlib__init__.py”, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File “<frozen importlib._bootstrap>”, line 1006, in _gcd_import File “<frozen importlib._bootstrap>”, line 983, in _find_and_load File “<frozen importlib._bootstrap>”, line 967, in _find_and_load_unlocked File “<frozen importlib._bootstrap>”, line 677, in _load_unlocked File “<frozen importlib._bootstrap_external>”, line 728, in exec_module File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\contrib\auth\models.py”, line 2, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\contrib\auth\base_user.py”, line 47, in <module>

class AbstractBaseUser(models.Model):

File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\db\models\base.py”, line 121, in new new_class.add_to_class(‘_meta’, Options(meta, app_label)) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\db\models\base.py”, line 325, in add_to_class value.contribute_to_class(cls, name) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\db\models\options.py”, line 208, in contribute_t o_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\db__init__.py”, line 28, in getattr return getattr(connections[DEFAULT_DB_ALIAS], item) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\db\utils.py”, line 207, in getitem backend = load_backend(db[‘ENGINE’]) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\db\utils.py”, line 111, in load_backend return import_module(‘%s.base’ % backend_name) File “H:\Coding\lib\importlib__init__.py”, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File “C:\Users\Shaun\PycharmProjects\Portfolio app\venv\lib\site-packages\django\db\backends\sqlite3\base.py”, line 14, in <modul e> from sqlite3 import dbapi2 as Database File “H:\Coding\lib\sqlite3__init__.py”, line 23, in <module> from sqlite3.dbapi2 import * File “H:\Coding\lib\sqlite3\dbapi2.py”, line 27, in <module> from _sqlite3 import * ImportError: DLL load failed: The specified module could not be found.``

And it is driving me absolutely insane.  I have tried installing sqlite3 manually or through pycharm and nothing works.
Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 21, 2020

Hi @littleblueline. It often helps to search for the last line of your error message online and look through the answers, e.g. in your case:

from _sqlite3 import * ImportError: DLL load failed: The specified module could not be found.

Reading over it, it seems that your system isn’t finding the database connector (sqlite3 for the default Django setup).

From your traceback I can also see that you’re on Windows. Are you working with the Anaconda Python distribution? I don’t see anything pointing clearly to that in your traceback, but still asking :) If you are, try creating your virtual environment with conda instead. You can read up on the steps for that in Creating development environments and installing Django with Anaconda.

I personally don’t have too much experience working with Python on Windows, and I’ve heard there are sometimes troubles brewing there…

Two ways that often work well for Python on Windows are:

  • Anaconda distribution: It comes pre-installed with a bunch of libraries geared towards data science, so not exactly what you’re looking for when doing web development.
  • Windows Store: You can now download Python 3 through the Windows store, which helps setting some PATH issues right from the start.

You could try those, or drill down some more on what exactly the issue is on your machine by doing some more research with the specifics of your system. E.g. when you include more search terms, such as the error message, “PyCharm”, your windows system and the way you installed Python, as well as how you created the venv etc.

Hope this helps, and let me know your system specifics and what you come up with in your search, and I can bounce back some more updated debugging ideas. :)

Avatar image for littleblueline

littleblueline on Feb. 22, 2020

Thank you for the advice, man. I uninstalled everything Python related on my computer as I had multiple versions, as well as Pycharm. I reinstalled everything today making sure to select the latest versions. It seems to be working fine now!

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 22, 2020

Yay, that’s great to hear :)

Avatar image for Brandy Wright

Brandy Wright on March 3, 2020

I dont see anyone having mentioned the error I’m getting so I’m adding that here in case someone can offer some insight while I keep working on it:

Django version 3.0.3, using settings 'portfolio.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[03/Mar/2020 13:22:47] "GET / HTTP/1.1" 404 2033
Not Found: /favicon.ico
[03/Mar/2020 13:22:48] "GET /favicon.ico HTTP/1.1" 404 2084

I see online articles for ‘creating a favicon.ico’ but I’m not keen to do that when it wasnt mentioned in this video…

Avatar image for Brandy Wright

Brandy Wright on March 3, 2020

Solved: Regarding the favicon.ico problem I describe above, I simply deleted the “portfolio” folder and the “manage.py” file from my django-portfolio directory and repeated these steps:

jango-admin startproject portfolio .
python manage.py runserver

Works fine now.

Maybe this will help someone else down the road.

Avatar image for Martin Breuss

Martin Breuss RP Team on March 5, 2020

Great! I’ve mentioned this in some other comments, but it seems that you downloaded the .zip file and started working with the already finished project. That file is meant only as a reference, for following this course you should build the code from scratch. Which is what you did here, by deleting the folder and re-creating it anew with the appropriate commands :)

Note: (just for the sake of being precise and avoiding confusion for other readers) It has to be:

django-admin startproject portfolio .

The d got lost somewhere on the way to our comment server 😜

Avatar image for Doug Ouverson

Doug Ouverson on Oct. 30, 2020

Great course Martin!

I was reading a section from docs.djangoproject.com/en/3.1/intro/tutorial01/about

Where should this code live?

If your background is in plain old PHP (with no use of modern frameworks), you’re probably used to putting code under the Web server’s document root (in a place such as /var/www). With Django, you don’t do that. It’s not a good idea to put any of this Python code within your Web server’s document root, because it risks the possibility that people may be able to view your code over the Web. That’s not good for security.

Put your code in some directory outside of the document root, such as /home/mycode.

Does django-admin startproject portfolio . place code in the Web server’s document root?

I was thinking that if you ran command without the “.” that the code would live outside the Web server’s document root?

Not sure what a best practice would be. So that when I’m ready to take a porject to the next phase I’ve got the file structure where I need it to be.

Thanks for your help Martin.

Avatar image for Martin Breuss

Martin Breuss RP Team on Oct. 31, 2020

Hi @Doug, the quick answer is that it doesn’t really matter.

Running the startproject command will create your project wherever your terminal is currently pointing to. That could theoretically be somewhere on your webserver, but for your own sanity’s sake I would suggest that you develop your project locally and only put it on a webserver when you’re done and everything works as you want it to : )

It helps to keep in mind that these are all just folder structure questions. Your local computer has a folder structure, your webserver has a folder structure, and your Django project has a folder structure as well. And you can move your Django project’s folder structure wherever you want to. It is self-contained.

When you set up your project on a webserver to get it online, you will have to do some work to point the webserver to your Django project.

Note that the second time I wrote “webserver” I’m talking about a program, such as nginx. Before I meant the server computer. To make your life working with the web a little bit more difficult, both the physical hardware as well as some programs that run on webservers to deliver content over the web are called “webservers”… 🙄 🤷‍♂️

tl;dr: As long as you don’t explicitly create your Django project in your server’s root directory, or copy it to there, using the . or not (which rhymes!) doesn’t make a difference here : )

Avatar image for Doug Ouverson

Doug Ouverson on Nov. 3, 2020

Hi @Martin

The reason I was concerned is that when developing a Drupal (a PHP-based CMS) site/app, the files are organized as such:

project/composer.json
project/composer.lock
project/.editorconfig
project/.gitattributes
project/vendor/
project/web/

All the Drupal files live inside web/

I could move all the Drupal files inside project/ but this is would make it very difficult to maintain site/app.

This is why I asked about Django folder structure. When app was ready for prime-time I wanted to have folders nice and tidy :)

Avatar image for KatMac

KatMac on May 22, 2021

I have installed your sample code into my editor and followed along with your code up to this point and everything is working as expected. However, when I clicked on the 127.0.0.1:8000/ link in my environment, the following error message displayed: How can I resolve this issue?

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/

Using the URLconf defined in portfolio.urls, Django tried these URL patterns, in this order:

    admin/
    projects/

The empty path didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on May 24, 2021

@KatMac The attached code is a finished “product,” which overrides Django’s default view that you can see in the video. Try visiting 127.0.0.1:8000/projects/ instead, or commenting out the highlighted line in portfolio/urls.py:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    # path('projects/', include('projects.urls')),
]

Become a Member to join the conversation.