Code Monkey home page Code Monkey logo

ms-django-tutorial's Introduction

Initial setup

Create and activate virtual environment:

# Windows
python -m venv .django-tutorial-env
.\\.django-tutorial-env\\Scripts\\Activate
pip install -r src/requirements.txt

# macOS or Linux
python3 -m venv .django-tutorial-env
source ./.django-tutorial-env/bin/activate
pip install -r src/requirements.txt

Create a project with Django-admin

django-admin startproject helloproject . 

The trailing period at the end of the command is important. It instructs django-admin to use the current folder. If you leave off the period, it will create an additional subdirectory.

Run the project

python src/manage.py runserver

Create app and register with project

Because apps and projects are separate in Django, you must register your app with the project. This is done by updating the INSTALLED_APPS variable inside settings.py for the project, adding a reference to the config class for the app. The config class is found in apps.py, and is the same name as the project. In our example, the class will be named HelloWorldConfig. E.g:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello_world.apps.HelloWorldConfig', # Function name at django-tutorial\hello_world\apps.py
]

Create paths and views

  1. open views.py, which will be inside hello_world
  2. Replace the code inside views.py with the following code. The helper function HttpResponse allows you to return text or other primitive types to the caller.
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world!")
  1. create a file in hello_world named urls.py with content
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
  1. Register our URLconf with the project. Our newly created URLconf is inside our hello_world application. Because the project controls all user requests, we need to register our URLconf in the core urls.py file, which is inside helloproject.
    1. Open urls.py inside helloproject
    from django.contrib import admin
    from django.urls import include, path
    
    urlpatterns = [
        path('', include('hello_world.urls')),
        path('admin/', admin.site.urls),
    ]
  2. Run your first app: python manage.py runserver

ms-django-tutorial's People

Contributors

sebestyenmiklos1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.