How to Start Django Project | Helloworld app Using Django Framework | Python Django tutorial for beginners | Getting started with Django | Django Framework | The ACX
1. Create a new virtual environment, install Django, and then activate it.
(pip install django)
2. Creating a project
(django-admin startproject mysite)
Let’s look at what startproject created:
These files are:
- manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
- __init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
- settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
- urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
- asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
- wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.
3. The development server
(python manage.py runserver)
4.Creating the Helloworld app
(python manage.py startapp Helloworld)
now, Your app directory should now look like:
- Open the file Helloworld/views.py and put the following Python code in it:
from django.http import HttpResponse
def index(request):
return HttpResponse("Helloworld")
- Then, Go to the Helloworld/urls.py file include the following code
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
- Next step is to point the root URLconf at the Helloworld.urls module. In mysite/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('Helloworld/', include('Helloworld.urls')),
path('admin/', admin.site.urls),
]
- Then run command (py manage.py runserver)
- (Go to http://localhost:8000/Helloworld/ in your browser, and you should see the text “Helloworld.", which you defined in the index view.)
- It is your output
Helloworld
Comments
Post a Comment