- Django 1.0 Website Development
- Ayman Hourieh
- 1176字
- 2021-05-21 20:19:58
Creating your first project
Now that the software we need is in place, here comes the fun part—creating our first Django project!
As you may recall from the Django installation section, we used a command called django-admin.py
to test our installation. This utility is at the heart of Django's project management facilities, as it enables the user to do a range of project management tasks. They include the following:
- Creating a new project
- Creating and managing the project's database
- Validating the current project and testing for errors
- Starting the development web server
In the rest of this chapter, we will see how to use some of these tasks and create a basis for our bookmark-sharing application in the process.
Creating an empty project
To create your first Django project, open a terminal (or command prompt for Windows users), type the following command, and hit Enter:
$ django-admin.py startproject django_bookmarks
This command will create a folder named django_bookmarks
in the current directory as well as the initial directory structure inside it. Let's see what kinds of files are created:
django_bookmarks/ __init__.py manage.py settings.py urls.py
Here is a quick explanation of what these files are:

When we start writing code for our application, we will create new files inside the project's folder. Thus, the folder also serves as a container for our code.
Setting up the database
In this section, we will work with code for the first time. Therefore, we will have to choose a source code editor to enter and edit the code. There are many options in the market when it comes to source code editors. Some people prefer fully fledged IDEs, whereas others like simple text editors. The choice is totally up to you—pick whichever you feel more comfortable with. If you already use a certain program to work with Python source files, then I suggest that you stick to it, as it will work just fine with Django. Otherwise, I can make a few recommendations:
- Scite (also known as Scintilla): This editor is lightweight, yet very powerful. It is available for Linux and Windows, supports syntax highlighting and code completion, and works well with Python. It is an open source editor, and you can find it at http://www.scintilla.org/SciTE.html.
- TextMate: This popular text editor for Mac OS X also provides a rich set of features for Django developers, while being user-friendly at the same time. TextMate is not free, but there is a thirty-day trial version that you can download from http://macromates.com/.
- Smultron: This is another popular Mac OS X open source editor and has a good Python support. You can download it at http://tuppis.com/smultron/.
- Eclipse + PyDev: This combination is an integrated development environment for Python. It supports all the standard features of IDEs from source version management to integrated debugging. It takes a while to learn all of its features, but for those who prefer a complete IDE (and especially those familiar with Eclipse), it is an excellent choice. More information on installation is available at http://pydev.sourceforge.net/.
Now that you have a source code editor ready, let's open the settings.py
file in the project folder and see what it contains:
# Django settings for django_bookmarks project. DEBUG = True TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', 'your_email@domain.com'), ) MANAGERS = ADMINS DATABASE_ENGINE = '' # 'postgresql_psycopg2', 'postgresql', # 'mysql', 'sqlite3' or 'ado_mssql'. DATABASE_NAME = '' # Or path to database file # if using sqlite3. DATABASE_USER = '' # Not used with sqlite3. DATABASE_PASSWORD = '' # Not used with sqlite3. DATABASE_HOST = '' # Set to empty string for localhost. # Not used with sqlite3. DATABASE_PORT = '' # Set to empty string for default. # Not used with sqlite3. # The rest of the file was trimmed.
As you may have already noticed, the file contains a number of variables that control various aspects of the application. Entering a new value for a variable is as simple as doing a Python assignment statement. In addition, the file is extensively commented. These comments explain what each variable controls.
What concerns us now is configuring the database. I mentioned earlier that Django supports several database systems, so first of all we have to specify the database system that we are going to use. This is controlled by the DATABASE_ENGINE
variable. As we are using SQLite, set this variable to 'sqlite3'
.
Next is the database name. We will choose a descriptive name for your database: Edit DATABASE_NAME
and set it to 'bookmarksdb'
. When using SQLite, this is all you need to do.
After those simple edits, the database section in settings.py
now looks as follows:
DATABASE_ENGINE = 'sqlite3' DATABASE_NAME = 'bookmarksdb' DATABASE_USER = '' DATABASE_PASSWORD = '' DATABASE_HOST = '' DATABASE_PORT = ''
Finally, we will tell Django to populate the configured database with tables. Although we haven't created any tables for our data yet (and we won't do so until the next chapter), Django requires several tables in the database for some of its features to function properly. Creating these tables is easy as it is only a matter of issuing the following command:
$ python manage.py syncdb
If you have entered this command and everything is correct, status messages will scroll on the screen indicating that the tables are being created. When prompted for the superuser account, enter your preferred username, email address, and password. It is important to create a superuser account, without which you won't be able to gain access to your initial web page once you have created it. If, on the other hand, the database is misconfigured, an error message will be printed to help you troubleshoot the issue. The command will create a file known as bookmarksdb
in your project directory. This file will contain your application's data.
With this done, we are ready to launch our application.
Launching the development server
As discussed earlier, Django comes with a lightweight web server for developing and testing applications. This server is pre-configured to work with Django, and more importantly, it restarts whenever you modify the code.
To start the server, run the following command:
$ python manage.py runserver
Next, open your browser, and navigate to http://localhost:8000/. You should see a welcome message as shown in the following screenshot:

Congratulations! You have created and configured your first Django project. This project will be the basis on which we will build our bookmarking application. In the next chapter, we will start developing our application. The page displayed by the web server will be replaced by something that we will have written ourselves!
- 中文版AutoCAD 2016從入門到精通
- 四時風(fēng)月繪:國風(fēng)水墨CG插畫繪畫技法
- 中文版After Effects CC實用教程
- 突破平面Premiere Pro 2022短視頻與視頻制作
- 中文版3ds Max 2024完全自學(xué)教程
- 超簡單的攝影后期書
- 改變思維:菜鳥也能做出震撼PPT(全彩版)
- 通達(dá)信炒股軟件從入門到精通(第2版)
- VRP11/3ds Max虛擬現(xiàn)實制作標(biāo)準(zhǔn)實訓(xùn)教程
- 中文版Photoshop CC基礎(chǔ)教程
- Python氣象應(yīng)用編程
- Learning Ext JS
- Joomla! E/Commerce with VirtueMart
- Oracle E/Business Suite R12 Supply Chain Management
- 中文版3ds Max 2016基礎(chǔ)培訓(xùn)教程