Commit 8bc60d05 authored by De Filippis Michael's avatar De Filippis Michael
Browse files

add ask page and success message

parent bc70e9a2
......@@ -30,4 +30,6 @@ from faq.webapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
url(r'^ask/', views.ask, name='ask'),
]
from django import forms
class QuestionForm(forms.Form):
your_question = forms.CharField(label='Your question', max_length=100)
......@@ -15,5 +15,9 @@
{% block navbar %}
{% endblock %}
{% block messages %}
{% endblock %}
{% block content %}
{% endblock %}
\ No newline at end of file
......@@ -15,6 +15,18 @@
</div>
{% endblock %}
{% block messages %}
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
{% block content %}
......
......@@ -10,7 +10,7 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class=""><a href="{% url 'index' %}">Home</a></li>
<li><a href="/kebab">Profile</a></li>
<li><a href="{% url 'ask' %}">Ask a question</a></li>
<li><a href="/tacos">Logout</a></li>
......
{% extends 'base.html' %}
{% load staticfiles%}
{% load static %}
{% block content %}
<div class="container well">
<form action="/ask/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="poster">
</form>
</div>
{% endblock %}
\ No newline at end of file
......@@ -11,6 +11,9 @@ from faq.webapp.serializers import UserSerializer, QuestionSerializer, GroupSeri
from faq.webapp.models import Question, Answer
from django.http import HttpResponse
from django.template import loader
from .forms import QuestionForm
from django.http import HttpResponseRedirect
from django.contrib import messages
class UserViewSet(viewsets.ModelViewSet):
......@@ -28,10 +31,12 @@ class GroupViewSet(viewsets.ModelViewSet):
queryset = Group.objects.all()
serializer_class = GroupSerializer
class QuestionViewSet(viewsets.ModelViewSet):
queryset = Question.objects.all().order_by('created')
serializer_class = QuestionSerializer
@login_required
def index(request):
answers = Answer.objects.all().order_by('question__created')
......@@ -39,3 +44,27 @@ def index(request):
'answers': answers,
}
return render(request, 'index.html', context)
def ask(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = QuestionForm(request.POST)
# check whether it's valid:
if form.is_valid():
messages.success(request, 'Votre question a été publiée')
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
questiontext = form.cleaned_data['your_question']
q = Question(question=questiontext)
q.save()
return HttpResponseRedirect('/')
# if a GET (or any other method) we'll create a blank form
else:
form = QuestionForm()
return render(request, 'question_form.html', {'form': form})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment