How to Create a Basic Project using MVT in Django ?

Prerequisite – Django Project MVT Structure

Assuming you have gone through the previous article. This article focusses on creating a basic project to render a template using MVT architecture. We will use MVT (Models, Views, Templates) to render data to a local server.

Create a basic Project:



  • To initiate a project of Django on Your PC, open Terminal and Enter the following command
    django-admin startproject projectName
  • A New Folder with name projectName will be created. To enter in the project using terminal enter command
    cd projectName
  • Create a new file views.py inside the project folder where settings.py, urls.py and other files are stored and save the following code in it-
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # HttpResponse is used to
    # pass the information 
    # back to view
    from django.http import HttpResponse
      
    # Defining a function which
    # will receive request and
    # perform task depending 
    # upon function definition
    def hello_geek (request) :
      
        # This will return Hello Geeks
        # string as HttpResponse
        return HttpResponse("Hello Geeks")

    chevron_right

    
    

  • Open urls.py inside project folder (projectName) and add your entry-
    • Import hello_geek function from views.py file.
      from projectName.views import hello_geeks

    • Add an entry in url field inside url patterns-
      path('geek/', hello_geek), 

  • Now to run the server follow these steps-
    • Open command prompt and change directory to env_site by this command-
      $ cd env_site
    • Go to Script directory inside env_site and activate virtual environment-
      $ cd Script
      $ activate
    • Return to the env_site directory and goto the project directory-
      $ cd ..
      $ cd geeks_site
    • Start the server- Start the server by typing following command in cmd-
      $ python manage.py runserver
    • Note- Take the help of previous django article if any issue arises in starting the server.

  • Checking – Open the browser and type this url-
    http://127.0.0.1:8000/geek/

  • Bingo…!! You are done with creating and rendering a basic Project.



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.