Skip to content

Instantly share code, notes, and snippets.

@cjadeveloper
Last active December 17, 2018 11:32
Show Gist options
  • Save cjadeveloper/a2e0e39cf21dcd55eb1b96bda903e90f to your computer and use it in GitHub Desktop.
Save cjadeveloper/a2e0e39cf21dcd55eb1b96bda903e90f to your computer and use it in GitHub Desktop.
Django 1.11 Templates, CRUD & Views
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<form method="GET" action="/blog/">
<input type="text" name="q" placeholder="Buscar">
</form>
<h1>Blog</h1>
{% for object in object_list %}
<li>
<a href="/blog/{{ object-id }}">{{ object.title }}</a>
</li>
{% endfor %}
from django.contrib.auth.decorators import login_required
from django.contrib.import messages
from django.db.models import Q
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
# Create your views here.
from .forms import PostModelForm
from .models import PostModel
def post_model_create_view(request):
form = PostModelForm(request.POST or None)
context = {
'form': form
}
if form.is_valid():
obj = form.save(commit=False)
# print(obj.title)
obj.save()
messages.success(request, 'Create a new post!')
context = {
"form": PostModelForm()
}
template = 'blog/create-view.html'
return render(request, template, context)
def post_model_update_view(request, id=None):
obj = get_object_or_404(PostModel, id=id)
form = PostModelForm(request.POST or None, instance=obj)
context = {
'form': form
}
if form.is_valid():
obj = form.save(commit=False)
# print(obj.title)
obj.save()
messages.success(request, 'Update post!')
return HttpResponseRedirect('/blog/{pk}'.format(pk=obj.id))
template = 'blog/update-view.html'
return render(request, template, context)
def post_model_delete_view(request, id=None):
obj = get_object_or_404(PostModel, id=id)
if request.method == 'POST':
obj.delete()
# podemos agregar el framework de messages de Django para manejar los mensajes
# https://docs.djangoproject.com/en/1.11/ref/contrib/messages/#module-django.contrib.messages
messages.success(request, 'Post deleted')
return HttpResponseRedirect('/blog/')
context = {
'object': obj,
}
template = 'blog/delete-view.html'
return render(request, template, context)
def post_model_detail_view(request, id=None):
obj = get_object_or_404(PostModel, id=id)
context = {
'object': obj,
}
template = 'blog/detail-view.html'
return render(request, template, context)
def post_model_list_view(request):
# Si tenemos un input de busqueda dentro de un form con name=q
# podemos hacer una busqueda, muy simplificada, definiendo una variable query
# más info de queries complejas https://docs.djangoproject.com/en/1.11/topics/db/queries/#complex-lookups-with-q-objects
query = request.GET.get('q', None)
qs = PostModel.objects.all()
if query is not None:
qs = qs.filter(
Q(title__icontains=query) |
Q(content__icontains=query) |
Q(slug__icontains=query)
)
context = {
'object_list': qs,
}
template = 'blog/list-view.html'
return render(request, template, context)
@login_required(login_url='/login/')
def login_required_view(request):
print(request.user)
qs = PostModel.objects.all()
context = {
'object_list': qs,
}
if request.user.is_authenticated():
template = 'blog/list-view.html'
else:
template = 'blog/list-view-public.html'
# raise Http404
return HttpResponseRedirect('/login/')
return render(request, template, context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment