demo-hackernews-with-django.../app/web/views.py

26 lines
787 B
Python
Raw Normal View History

2021-06-25 14:25:14 +02:00
from django.shortcuts import render, redirect, reverse
from app.api.models import News
from app.web.forms import NewsForm
2021-06-22 15:43:26 +02:00
def home(request):
2021-06-23 15:10:44 +02:00
return render(request, 'pages/list.html', {})
2021-06-22 15:43:26 +02:00
def add_news(request):
2021-06-25 14:25:14 +02:00
mi_form = NewsForm()
# Validamos el formulario
if request.method == 'POST':
mi_form = NewsForm(request.POST)
# Muestro los errores
if mi_form.is_valid():
# Guardo
mi_new_news = News()
mi_new_news.title = mi_form.cleaned_data.get('title')
mi_new_news.url = mi_form.cleaned_data.get('url')
mi_new_news.save()
# Redirecciono a Home
return redirect(reverse('list'))
return render(request, 'pages/add-news.html', {
'news_form': mi_form
})