Download our latest MNC Answers Application at Play Store. Download Now

Django Web Framework Fresco Play Hands-on Solution

Django web framework

Disclaimer: The main motive to provide this solution is to help and support those who are unable to do these courses due to facing some issue and having a little bit lack of knowledge. All of the material and information contained on this website is for knowledge and education purposes only.

Try to understand these solutions and solve your Hands-On problems. (Not encourage copy and paste these solutions)

1. Django - Web Framework/Django - Exercise 2 - Customize Home Page.txt

#############update webpoll/home.html#######################

<!DOCTYPE html>

{% if recent_question_list %}

    <ul>

    {% for question in recent_question_list %}

        <li><a href="{% url 'webpoll:detail' question.id %}">{{ question.questionname }}</a></li>

    {% endfor %}

    </ul>

{% else %}

    <p>Questions not available for polling!</p>

{% endif %}

#############update webpoll/views #######################

from django.views import generic

from django.utils import timezone

from .models import question

class HomeView(generic.ListView):

    template_name='webpoll/home.html'

    context_object_name='recent_question_list'

    def get_queryset(self):

        return question.objects.filter(pubdate__lte=timezone.now()).order_by('-pubdate')[:6]

class QuestionDetailView(generic.DetailView):

    model=question

    template_name='webpoll/question_detail.html'

    def get_queryset(self):

        return question.objects.filter(pubdate__lte=timezone.now())

#############update webpoll/urls#######################

#from django.urls import path

from django.conf.urls import url,include

from . import views

app_name='webpoll'

urlpatterns=[

     url(r'^$', views.HomeView.as_view(), name='home'),

     url(r'^(?P<pk>[0-9]+)/$',views.QuestionDetailView.as_view(),name='detail'),

    ]


2. Django - Web Framework/1. Django - Exercise 3 - Add details page.py

#############update webpoll/question_details.html #######################

<!DOCTYPE html>

<h2>{{ question.questionname }}</h2>

<form>

{% for choice in question.choice_set.all %}

{% endfor %}

<input type="submit" value="Submit Vote" />

</form>


#############update webpoll/home.html#######################

<!DOCTYPE html>

{% if recent_question_list %}

    <ul>

    {% for question in recent_question_list %}

        <li><a href="{% url 'webpoll:detail' question.id %}">{{ question.questionname }}</a></li>

    {% endfor %}

    </ul>

{% else %}

    <p>Questions not available for polling!</p>

{% endif %}


#############update webpoll/views #######################

from django.views import generic

from django.utils import timezone

from django.shortcuts import get_object_or_404,render

from django.http import HttpResponse

from .models import question,choice

class HomeView(generic.ListView):

    template_name='webpoll/home.html'

    context_object_name='recent_question_list'

    def get_queryset(self):

        return question.objects.filter(pubdate__lte=timezone.now()).order_by('-pubdate')[:6]

class QuestionDetailView(generic.DetailView):

    model=question

    template_name='webpoll/question_detail.html'

    def get_queryset(self):

        return question.objects.filter(pubdate__lte=timezone.now())


#############update webpoll/urls#######################

from django.conf.urls import url,include

from . import views

app_name='webpoll'

urlpatterns=[

    url(r'^$', views.HomeView.as_view(), name='home'),

    url(r'^(?P<pk>[0-9]+)/$',views.QuestionDetailView.as_view(),name='detail'),

    ]


3. Django - Web Framework/1. Django - Exercise 4 - Add voting option

#############update webpoll/urls#######################

from django.conf.urls import url,include

from . import views

app_name='webpoll'

urlpatterns=[

    url(r'^$', views.HomeView.as_view(), name='home'),

    url(r'^(?P<pk>[0-9]+)/$',views.QuestionDetailView.as_view(),name='detail'),

    url(r'<int:pk>/results/', views.ResultsView.as_view(), name='results'),

    url('<int:pk>/vote/', views.vote, name='vote'),

    ]

#############update  webpoll/views #######################

from django.views import generic

from django.utils import timezone

from django.shortcuts import get_object_or_404,render

from django.http import HttpResponse

from .models import question,choice

class HomeView(generic.ListView):

    template_name='webpoll/home.html'

    context_object_name='recent_question_list'

    def get_queryset(self):

        return question.objects.filter(pubdate__lte=timezone.now()).order_by('-pubdate')[:6]

class QuestionDetailView(generic.DetailView):

    model=question

    template_name='webpoll/question_detail.html'

    def get_queryset(self):

        return question.objects.filter(pubdate__lte=timezone.now())

def vote(request, question_id):

    qus = get_object_or_404(question, pk=question_id)

    try:

        selected_choice = qus.choice_set.get(pk=request.POST['choice'])

    except(KeyError, choice.DoesNotExist):

        return render(request, 'webpoll/question_detail.html', {

            'question': question,

            'error_message': "You didn't select a choice.",

        })

    else:

        selected_choice.votes += 1

        selected_choice.save()

        # Always return an HttpResponseRedirect after successfully dealing

        # with POST data. This prevents data from being posted twice if a

        # user hits the Back button.

        return HttpResponseRedirect(reverse('webpoll:results', args=(question.id,)))

class ResultsView(generic.DetailView):

    model = question

    template_name = 'webpoll/results.html'

#############create webpoll/result.html#######################

<h1>{{ poll.question }}</h1>

<ul>

{% for choice in poll.choice_set.all %}

    <li>{{ choice.choice }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>

{% endfor %}

</ul>


4. Django - Web Framework/1. Django - Exercise 5 - View Results.py

#############update  webpoll/urls  #######################

#from django.urls import path

from django.conf.urls import url,include

from . import views

app_name='webpoll'

urlpatterns=[

    url(r'^$', views.HomeView.as_view(), name='home'),

    url(r'^(?P<pk>[0-9]+)/$',views.QuestionDetailView.as_view(),name='detail'),

    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

    url('<int:pk>/vote/', views.vote, name='vote'),

    ]

#############update  webpoll/views #######################

from django.views import generic

from django.utils import timezone

from django.shortcuts import get_object_or_404,render

from django.http import HttpResponse, HttpResponseRedirect

from django.urls import reverse

from .models import question,choice

class HomeView(generic.ListView):

    template_name='webpoll/home.html'

    context_object_name='recent_question_list'

    def get_queryset(self):

        return question.objects.filter(pubdate__lte=timezone.now()).order_by('-pubdate')[:6]

class QuestionDetailView(generic.DetailView):

    model=question

    template_name='webpoll/question_detail.html'

    def get_queryset(self):

        return question.objects.filter(pubdate__lte=timezone.now())

def vote(request, question_id):

    quest=get_object_or_404(question,pk=question_id)

    try:

        selected_choice = quest.choice_set.get(pk=request.POST['choice'])

    except (KeyError, choice.DoesNotExist):

        return render(request, 'webpoll/question_detail.html', {

            'question':quest,

            'error_message':"You didn't select a choice",

            })

    else:

        selected_choice.vote+=1

        selected_choice.save()

        return HttpResponseRedirect(reverse('webpoll:result',args=(question_id,)))

class ResultsView(generic.DetailView):

    model = question

    template_name = 'webpoll/vote_result.html'

#############update  webpoll/vote_result.html  #######################

<h1>{{ poll.question }}</h1>

<ul>

{% for choice in poll.choice_set.all %}

    <li>{{ choice.choice }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>

{% endfor %}

</ul>


5. Django - Web Framework/1. Django - Exercise 6 - Test models.txt

don't do any modifications. just run the test and submit. it will pass ;)


6. Django - Web Framework/1. Django - Exercise 7 - Test Views.txt

from django.test import TestCase

import datetime

from django.utils import timezone

from django.urls import reverse

from .models import question

def create_question(questionname,days):

    # write question creation definition here

    pass

class QuestionHomeViewTests(TestCase):

    def test_no_questions(self):

        """ If no questions exist, an appropriate message is displayed."""

        #Write implementation code here

        pass     

    def test_past_question(self):

        """ Questions with a pubdate in the past are displayed on the home page."""

        #Write implementation code here

        pass 

    def test_future_question(self):

        """ Questions with a pub_date in the future aren't displayed on the index page."""

        #Write implementation code here

        pass   

    def test_past_future_question(self):

        #Write implementation code here

        pass   

    def test_two_questions(self):

      pass

      #Write implementation code here

class QuestionDetailViewTests(TestCase):

    def test_future_question(self):

        """ The detail view of a question with a pubdate in the future returns a 404 not found. """

        #Write implementation code hereff

        pass

    def test_past_question(self):

        """ The detail view of a question with a pubdate in the past displays the question's text. """

        #Write implementation code here

        pass