avancé de dev

This commit is contained in:
Ambulance Clerc
2022-02-15 14:48:18 +01:00
parent d84d7d9823
commit c990f87413
162 changed files with 27899 additions and 27 deletions

View File

@@ -4,7 +4,19 @@ from polls.models import Question, Choice
from vehicles.models import *
# Register your models here.
admin.site.register(Question)
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_txt']}),
('Date information', {'fields': ['pub_date']}),
]
inlines = [ChoiceInline]
list_display = ('question_txt', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
search_fields = ['question_txt']
admin.site.register(Question, QuestionAdmin)
admin.site.register(Choice)
admin.site.register(Vehicles)

View File

@@ -0,0 +1,28 @@
# Generated by Django 4.0 on 2022-01-31 14:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('polls', '0003_delete_vehicles'),
]
operations = [
migrations.AlterField(
model_name='choice',
name='choice_txt',
field=models.CharField(max_length=200, verbose_name='Nom'),
),
migrations.AlterField(
model_name='question',
name='pub_date',
field=models.DateTimeField(verbose_name='Date de publication'),
),
migrations.AlterField(
model_name='question',
name='question_txt',
field=models.CharField(max_length=250, verbose_name='Description'),
),
]

View File

@@ -1,23 +1,28 @@
import datetime
import datetime
from django.db import models
from django.utils import timezone
from django.contrib import admin
# Create your models here.
class Question(models.Model):
question_txt = models.CharField( max_length=250)
pub_date = models.DateTimeField('date published')
question_txt = models.CharField('Description', max_length=250)
pub_date = models.DateTimeField('Date de publication')
def __str__(self):
return self.question_txt
@admin.display(boolean=True, ordering='pub_date', description='Publié récamment')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_txt = models.CharField( max_length=200)
choice_txt = models.CharField("Nom", max_length=200)
votes = models.IntegerField( default=0)
def __str__(self):

View File

@@ -1,6 +1,12 @@
<h1>{{ question.question_txt }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_txt }}</li>
{% endfor %}
</ul>
<form action="{% url 'polls:vote' question.id %}" method="post">
{%csrf_token%}
<fieldset>
<legend><h1>{{question.question_txt}}</h1></legend>
{%if error_message%}<p><strong>{{error_message}}</strong></p>{%endif%}
{%for choice in question.choice_set.all%}
<input type="radio" name="choice" id="choice{{forloop.counter}}" value="{{choice.id}}" />
<label for="choice{{forlopp.counter}}">{{choice.choice_txt}}</label><br>
{% endfor %}
</fieldset>
<input type="submit" value="Vote">
</form>

View File

@@ -1,8 +1,11 @@
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
{% if latest_question_list %}
<h1>Liste des questions:</h1>
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}/">{{ question.question_txt }}</a></li>
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_txt }}</a></li>
{% endfor %}
</ul>
{% else %}

View File

@@ -5,11 +5,8 @@ from . import views
app_name = "polls"
urlpatterns = [
path('',views.index, name='index'),
# ex: /polls/5/
path('specifics/<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
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'),
]
]

View File

@@ -1,10 +1,44 @@
from django.shortcuts import get_object_or_404, render
from django.http import Http404
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from polls.models import *
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request, 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,)))
'''
def index(request):
latest_question_list = Question.objects.order_by("-pub_date")[:5]
@@ -22,5 +56,11 @@ def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("Tu réponds à la question %s." % question_id)
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})
'''