Starting a Project in django

Starting a Project

Once you’ve installed Python and Django, you can take the first step in developing a Django application by creating a Django project.
A Django project is a collection of settings and files for a single Django website. To create a new Django project we’ll be using a special command to auto-generate the folders, files and code that make up a Django project. This includes a collection of settings for an instance of Django, database configuration, Django-specific options and application-specific settings.
I am assuming at this stage you are still running the virtual environment from the previous installation step. If not, you will have to start it again with env_myclub\scripts\activate\.
From your virtual environment command line, run the following command:
django-admin startproject myclub_site
This command will automatically create a myclub_site folder in your project folder, as well as all the necessary files for a basic, but fully functioning Django website. Feel free to explore what startproject created now if you wish, however, we will be going into greater detail on the structure of a Django project in the next chapter.

Creating a Database

Django includes several applications by default (e.g., the admin program and user management and authentication). Some of these applications makes use of at least one database table, so we need to create tables in the project database before we can use them. To do this, change into the myclub_site folder created in the last step (type cd myclub_site at the command prompt) and run the following command:
python manage.py migrate
The migrate command creates a new SQLite database and any necessary database tables, according to the settings file created by the startproject command (more on the settings file later). If all goes to plan, you’ll see a message for each migration it applies:
(env_myclub) ...\myclub_site>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  # several more migrations (not shown)

The Development Server

Let’s verify your Django project works. Make sure you are in the outer myclub_site directory and run the following command:
python manage.py runserver 
This will start the Django development server—a lightweight Web server written in Python. The development server was created so you can develop things rapidly, without having to deal with configuring a production server until you’re ready for deployment.
When the server starts, Django will output a few messages before telling you that the development server is up and running at http://127.0.0.1:8000/. If you were wondering, 127.0.0.1 is the IP address for local host, or your local computer. The 8000 on the end is telling you that Django is listening at port 8000 on your local host.
You can change the port number if you want to, but I have never found a good reason to change it, so best to keep it simple and leave it at the default.
Now that the server is running, visit http://127.0.0.1:8000/ with your web browser. You’ll see Django’s default welcome page, complete with a cool animated rocket (Figure 2-6).
It worked!
Figure 2.6: Django’s welcome page

Post a Comment

0 Comments