Add author selection form
This commit is contained in:
parent
aabd1c301a
commit
c24608440f
|
@ -1,5 +1,5 @@
|
|||
from django.forms import ModelForm, CharField, EmailField
|
||||
from .models import Suggestion
|
||||
from django.forms import ModelForm, Form, CharField, EmailField, ModelChoiceField
|
||||
from .models import Suggestion, Author
|
||||
|
||||
|
||||
class SuggestionForm(ModelForm):
|
||||
|
@ -10,5 +10,23 @@ class SuggestionForm(ModelForm):
|
|||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.fields['suggester_name'] = CharField(label='Tu nombre')
|
||||
self.fields['suggester_email'] = EmailField(label='Tu email')
|
||||
self.fields['suggester_name'] = CharField(
|
||||
label='Tu nombre')
|
||||
self.fields['suggester_name'].widget.attrs['placeholder'] = "Pepito Grillo"
|
||||
|
||||
self.fields['suggester_email'] = EmailField(
|
||||
label='Tu email')
|
||||
self.fields['suggester_email'].widget.attrs['placeholder'] = "pepitog@fakemail.com"
|
||||
|
||||
# Change labels to spanish
|
||||
self.fields['text'].label = "Cita"
|
||||
self.fields['text'].widget.attrs['placeholder'] = "¡Vamos que nos pilla el comunismo!"
|
||||
self.fields['context'].label = "Contexto"
|
||||
self.fields['context'].widget.attrs['placeholder'] = "Estaba tardando mucho en hacerle caso a Lázaro"
|
||||
self.fields['author_name'].label = "Nombre de la persona que citas"
|
||||
self.fields['author_name'].widget.attrs['placeholder'] = "Lázaro"
|
||||
|
||||
|
||||
class AuthorSelectionForm(Form):
|
||||
author = ModelChoiceField(
|
||||
queryset=Author.objects.all(), empty_label="Selecciona un autor")
|
||||
|
|
|
@ -6,9 +6,10 @@ app_name = "lazaro_quotes"
|
|||
|
||||
urlpatterns = [
|
||||
path("", views.show_quote, name="show_quote"),
|
||||
path("suggest", views.suggest_quote_form, name="suggest_quote_form"),
|
||||
path("select_author", views.select_author_form, name="select_author_form"),
|
||||
path("json", views.get_quote_json, name="get_quote_json"),
|
||||
path("all/json", views.get_all_quotes_json, name="get_all_quotes_json"),
|
||||
path("<str:author_slug>", views.show_quote, name="show_quote"),
|
||||
path("<str:author_slug>/json", views.get_quote_json, name="get_quote_json"),
|
||||
path("suggest", views.suggest_quote_form, name="suggest_quote_form")
|
||||
path("<str:author_slug>/json", views.get_quote_json, name="get_quote_json")
|
||||
]
|
||||
|
|
|
@ -4,7 +4,7 @@ from django.forms.models import model_to_dict
|
|||
from django.http import JsonResponse
|
||||
|
||||
from .models import Quote, Suggester
|
||||
from .forms import SuggestionForm
|
||||
from .forms import SuggestionForm, AuthorSelectionForm
|
||||
|
||||
|
||||
def show_quote(request, author_slug=None):
|
||||
|
@ -37,6 +37,18 @@ def get_all_quotes_json(request):
|
|||
return JsonResponse(result)
|
||||
|
||||
|
||||
def select_author_form(request):
|
||||
if request.method == 'POST':
|
||||
form = AuthorSelectionForm(request.POST)
|
||||
|
||||
if form.is_valid():
|
||||
return redirect('lazaro_quotes:show_quote', form.cleaned_data['author'].slug)
|
||||
else:
|
||||
form = AuthorSelectionForm()
|
||||
|
||||
return render(request, 'main/show_form.html', {'form_header': 'Selecciona una persona para obtener una cita suya', 'form': form})
|
||||
|
||||
|
||||
def suggest_quote_form(request):
|
||||
if request.method == 'POST':
|
||||
form = SuggestionForm(request.POST)
|
||||
|
|
|
@ -59,8 +59,11 @@
|
|||
aria-haspopup="true">Lázaro-aaS
|
||||
</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a class="dropdown-item nav-link" href="{% url 'lazaro_quotes:show_quote' %}">Frase
|
||||
<li><a class="dropdown-item nav-link" href="{% url 'lazaro_quotes:show_quote' %}">Cita
|
||||
aleatoria</a></li>
|
||||
<li><a class="dropdown-item nav-link"
|
||||
href="{% url 'lazaro_quotes:select_author_form' %}">Seleccionar un autor de cita</a>
|
||||
</li>
|
||||
<li><a class="dropdown-item nav-link"
|
||||
href="{% url 'lazaro_quotes:suggest_quote_form' %}">Sugerir
|
||||
frases</a></li>
|
||||
|
|
Loading…
Reference in a new issue