Django can have two views py files

MicrosoftDocs /

Django tutorial in Visual Studio, step 6: Survey project template
This walkthrough provides you with basic information about Django in the context of Visual Studio projects, especially features of the "Queries a Django web project" template, such as administrative customization.

Previous step: Authenticating users to Django

Now that you have successfully dealt with the Django Web Project template, you can now look at the third Django template, Queries a Django Web Project, which is based on the same code and illustrates working with a database.

This step explains the following:

[! div class = "checklist"]

  • Create a project from the template and initialize the database (step 6-1)
  • Information about data models (step 6-2)
  • Apply Migrations (Step 6-3)
  • Information on the views and page templates created by the project template (step 6-4)
  • Create a Custom Administrative Interface (Step 6-5)

A project created with this template is similar to the result you get by following the Writing your first Django app tutorial in the Django Documents. The web app consists of a public website that allows polls to be viewed and voted on, and a custom management interface that allows you to manage polls. It uses the same authentication system as the Django Web Project template and also uses the database by implementing Django models, which the following sections deal with.

Step 6-1: Create the project and initialize the database

  1. In Visual Studio, switch to the Solution Explorer. Right-click the solution LearningDjangothat you have already created in this tutorial. Then click on Add > New project. (If you'd like to use a new solution instead, click file > New > Project.)

  2. In the New Project dialog box, find the template Queries a Django web projectand select it. Name the project DjangoPolls and click OK.

  3. Like the other project templates in Visual Studio, the Query a Django Web Project template includes a file named requirements.txt. A Visual Studio prompt asks where to install these dependencies. Select the option Install in a virtual environment out. Click in the dialog box Add a virtual environment on Createto accept the default values.

  4. Once Python has finished setting up the virtual environment, follow the instructions in the readme.htmlto initialize the database and create a Django administrator. First, right-click the project DjangoPolls in the Solution Explorer, and choose the command python > Migrate to Django out. Then right-click the project again and select the command python > Create a Django administrator and follow the prompts. (If you try to create an administrator first, you will get an error because the database has not yet been initialized.)

  5. Put the project DjangoPolls as the default project for the Visual Studio solution. To do this, right-click the Solution Explorer on the project and choose the option Set as Startup Project (Set as start project). The startup project shown in bold is executed when you start the debugger.

  6. Select to run the server Debug > Start debugging (F5) or use the button Web server on the toolbar:

  7. The app created from the template has three pages: "Home", "About" and "Contact". You can navigate between these pages using the top navigation bar. Take a few minutes to examine the different parts of the app (the About and Contact us pages are very similar to Django Web Project and will not be discussed further).

  8. Also choose the link administration in the navigation bar to display a login screen to show that only authenticated users have authority for the administrative interface. Use administrator credentials. You will be redirected to the / admin page which is enabled by default when using this project template.

  9. The app can continue to run for the following sections.

    If you want to exit the app and commit the changes to source control, first open the Team Explorer the page Changes. Right-click the folder for the virtual environment (presumably env) and select the option Ignore these local elements out.

Review the project content

As already mentioned. Many of the parts of a project built from the Queries Django Web Project template should be familiar to you after exploring the other project templates in Visual Studio. The additional steps in this article summarize the more important changes and additions: data models and additional views.

Question: What does the Migrate Django command do?

Answer: The order Migrate to Django runs the command that contains all of the scripts in the folder app / migrations runs that have not been run before. In this case, the command runs the script in this folder to set up the required schema in the database.

The migration script itself is created by the command that created the file the app, compares it with the current status of the database and then generates the necessary scripts to migrate the database schema according to the current models. This feature of Django is very powerful as you update and change your models over time. By generating and executing migrations, the models and the database always remain synchronized with little effort.

You are working with a migration in step 6-3 later in this article.