This is going to be a different project: We will call it the Poll project
Creating a project¶
If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.
django-admin startproject mysite
This will create a mysite directory in your current directory
ASfter that we will run the server inmediatley
If we click the direction in blue , this is what we will see.
Creating the Polls app¶
Now that your environment – a “project” – is set up, you’re set to start doing work.Each application you write in Django consists of a Python package that follows a certain convention. Django comes with a utility that automatically generates the basic directory structure of an app, so you can focus on writing code rather than creating directories.
Write our first view¶
Let’s write the first view. Open the file
polls/views.py and put the following Python code in it:
To call the view, we need to map it to a URL - and for this we need a URLconf.
To create a URLconf in the polls directory, create a file called
urls.py. Your app directory should now look like:
In the
polls/urls.py file include the following code:from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'),
]
The next step is to point the root URLconf at thepolls.urlsmodule. Inmysite/urls.py, add an import fordjango.urls.includeand insert aninclude()in theurlpatternslist, so you have:
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ]
You should always use
include() when you include other URL patterns. admin.site.urls is the only exception to this.
Now run : py manage.py runserver in terminal
You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them.
Now please check this site with the correct url
Poll Project
Reviewed by ohhhvictor
on
June 08, 2019
Rating:
Reviewed by ohhhvictor
on
June 08, 2019
Rating:














No comments: