Thursday, June 13, 2019

Create app in Django python





Create project in Django
django-admin startproject PROJECT_NAME
django-admin startproject learnpython
go to directory (cd learnpython)
run python (python manage.py runserver)
Python project is start. Default url should be http://127.0.0.1:8000/

For more details about project install  click here
Create App
     python manage.py startapp firstapp
Now its time to start working in your first app.

Now we will work in our firstapp directory
Modify its view file fist (/firstapp/views.py)
      from django.http import HttpResponse


def index(request):

    return HttpResponse("Wow! Writing First Web App In Django!")

Now we need to define before use it.
For using it we need to create “urls.py” file under firstapp directory.
        from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
]

Now our app is ready to use.
But our project should inform that we have created new app.
In our project “urls.py”. We need to define our app path in this file.
        from django.contrib import admin
from django.urls import include, path
urlpatterns = [
    path('firstapp/', include('firstapp.urls')),
    path('admin/', admin.site.urls),
]

Now our project understand about our created new “firstapp”.
Now start our project and check with this new app.
python manage.py runserver

Enjoy your first app!!

0 comments:

Post a Comment