[Django #5] Tutorial 4
This commit is contained in:
parent
5fd4e6fbab
commit
f270905d01
|
@ -25,8 +25,8 @@ SECRET_KEY = '2kbmvs@pcbxn)kt#p9$)45=h#i8_b7^^l&l57mbe4%e=+k91-d'
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
|
||||||
ALLOWED_HOSTS = [ "[2a0a:e5c1:151::12]" ]
|
ALLOWED_HOSTS = [ "[2a0a:e5c1:114::12]", "127.0.0.1", "[2a0a:e5c1:151::12]", "testjg.django.lab.ungleich.ch", "[2a0a:e5c1:114::17]" ]
|
||||||
|
#ALLOWED_HOSTS = []
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,11 @@ Including another URLconf
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path
|
from django.urls import include, path
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
path('polls/', include('polls.urls')),
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from .models import Question
|
from .models import Question, Choice
|
||||||
|
|
||||||
admin.site.register(Question)
|
admin.site.register(Question)
|
||||||
|
admin.site.register(Choice)
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
# Generated by Django 3.0.8 on 2020-07-06 15:51
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Question',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('question_text', models.CharField(max_length=200)),
|
||||||
|
('pub_date', models.DateTimeField(verbose_name='date published')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Choice',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('choice_text', models.CharField(max_length=200)),
|
||||||
|
('votes', models.IntegerField(default=0)),
|
||||||
|
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,12 @@
|
||||||
|
<h1>{{ question.question_text }}</h1>
|
||||||
|
|
||||||
|
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
|
||||||
|
|
||||||
|
<form action="{% url 'polls:vote' question.id %}" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{% for choice in question.choice_set.all %}
|
||||||
|
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
|
||||||
|
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
|
||||||
|
{% endfor %}
|
||||||
|
<input type="submit" value="Vote">
|
||||||
|
</form>
|
|
@ -0,0 +1,9 @@
|
||||||
|
{% if latest_question_list %}
|
||||||
|
<ul>
|
||||||
|
{% for question in latest_question_list %}
|
||||||
|
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% else %}
|
||||||
|
<p>No polls are available.</p>
|
||||||
|
{% endif %}
|
|
@ -0,0 +1,9 @@
|
||||||
|
<h1>{{ question.question_text }}</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for choice in question.choice_set.all %}
|
||||||
|
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
|
|
@ -2,6 +2,10 @@ from django.urls import path
|
||||||
|
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
app_name = 'polls'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.index, name='index'),
|
path('', views.IndexView.as_view(), name='index'),
|
||||||
|
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
||||||
|
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
|
||||||
|
path('<int:question_id>/vote/', views.vote, name='vote'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,3 +1,59 @@
|
||||||
from django.shortcuts import render
|
from django.http import HttpResponse, HttpResponseRedirect
|
||||||
|
#from django.http import Http404
|
||||||
|
from django.template import loader
|
||||||
|
from django.shortcuts import get_object_or_404, render
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.views import generic
|
||||||
|
|
||||||
# Create your views here.
|
from .models import Choice, Question
|
||||||
|
|
||||||
|
|
||||||
|
#def index(request):
|
||||||
|
# latest_question_list = Question.objects.order_by('-pub_date')[:5]
|
||||||
|
# context = {'latest_question_list': latest_question_list}
|
||||||
|
# return render(request, 'polls/index.html', context)
|
||||||
|
class IndexView(generic.ListView):
|
||||||
|
template_name = 'polls/index.html'
|
||||||
|
context_object_name = 'latest_question_list'
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""Return the last five published questions."""
|
||||||
|
return Question.objects.order_by('-pub_date')[:5]
|
||||||
|
|
||||||
|
#def detail(request, question_id):
|
||||||
|
# #return HttpResponse("You're looking at question %s." % question_id)
|
||||||
|
# question = get_object_or_404(Question, pk=question_id)
|
||||||
|
# return render(request, 'polls/detail.html', {'question': question})
|
||||||
|
class DetailView(generic.DetailView):
|
||||||
|
model = Question
|
||||||
|
template_name = 'polls/detail.html'
|
||||||
|
|
||||||
|
|
||||||
|
#def results(request, question_id):
|
||||||
|
# #response = "You're looking at the results of question %s."
|
||||||
|
# #return HttpResponse(response % question_id)
|
||||||
|
# question = get_object_or_404(Question, pk=question_id)
|
||||||
|
# return render(request, 'polls/results.html', {'question': question})
|
||||||
|
class ResultsView(generic.DetailView):
|
||||||
|
model = Question
|
||||||
|
template_name = 'polls/results.html'
|
||||||
|
|
||||||
|
|
||||||
|
def vote(request, question_id):
|
||||||
|
#return HttpResponse("You're voting on question %s." % question_id)
|
||||||
|
question = get_object_or_404(Question, pk=question_id)
|
||||||
|
try:
|
||||||
|
selected_choice = question.choice_set.get(pk=request.POST['choice'])
|
||||||
|
except (KeyError, Choice.DoesNotExist):
|
||||||
|
# Redisplay the question voting form.
|
||||||
|
return render(request, 'polls/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('polls:results', args=(question.id,)))
|
||||||
|
|
Loading…
Reference in New Issue