Search This Blog

Welcome to Django - Exercise 1 - Create a basic webpage - Create a basic Django powered webpage. The following are provided - hackerrank questions and answers fresco play

1. Django - Exercise 1 - Create a basic Webpage

Create a basic Django powered webpage. The following are provided: 

  • 1. Project - Exercise 1 
  • 2. App - Webpoll 
  • 3. Models - Question and Choice 
  • 4. Migrated models 

Do the following: 

1. In views.py, define a `home` function that accepts a `request` and returns an HttpResponse with the text "Welcome to webpoll home!" 

2. In webpoll/urls.py: Import views Create a `urlpatterns`variable. Add a URL with route '' to `views.home` named name='home'. 3. In exercise1/urls.py: 

3. In exercise1/urls.py: 

Import `include` from `django.urls`. 

Create a urlpatterns variable. 

Add a path to set the home URL as 'webpoll/'. 

Call the `webpoll.urls` using the include function. 

NoteTo run migrations again, use the in-built command line option by clicking "New Terminal" (">_"), or by clicking "File" -> "New Terminal". 

In the IDE, do the following: 

1. Click RUN -> INSTALL (to install the necessary packages). 

2. Click RUN -> RUN SERVER (to launch Jupyter notebook). 

3. Click Preview App. 

4. Click RUN -> Tests (to test the application). Note: To check the functioning of the web page, use: /webpoll 

Note: To check the functioning of the web page, use: <preview app url>/webpoll

5. Click Submit Code and Continue (to submit the exercise). 


Answer:

webpoll/urls.py

from django.conf.urls import url
from . import views

urlpatterns=[
    # URL pattern to home page
    url(r'', views.home, name="home"),
    ]


webpoll/views.py

from django.http import HttpResponse

def home(request):
    #Write code implementation here
    return HttpResponse("Welcome to webpoll home!")


exercise1/urls.py

"""exercise1 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.conf.urls import url,include


urlpatterns = [
    # URL pattern to include webpoll/urls
    url(r'^webpoll/', include('webpoll.urls')),
    url(r'^admin/', admin.site.urls),
]


Click on the image to see tests: 









No comments:

Post a Comment

If you have any doubts, Please let us know.