Created
March 20, 2018 09:13
-
-
Save m-root/7a3db49a0e032d5141931e3a152337f8 to your computer and use it in GitHub Desktop.
Django class CURD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################### MODELS ########################## | |
class ModelAuthor(models.Model): | |
username = models.CharField(max_length=150, null=False, blank=False) | |
def __str__(self): | |
return self.username # This gets called for example when you create an user input, the text shown is the returned string | |
class ModelPost(models.Model): | |
title = models.CharField(max_length=200) | |
author = models.ForeignKey(ModelAuthor) | |
body = models.TextField() | |
created = models.DateField(auto_now_add=True) | |
updated = models.DateField(auto_now=True) | |
def __str__(self): | |
return "post from {} created at {}".format(self.author, self.created) | |
####################### VIEWS ########################## | |
from django.http import HttpResponseRedirect | |
from demos.models import ModelPost | |
from django.core.urlresolvers import reverse | |
from django.views.generic import CreateView, UpdateView, DetailView, DeleteView, ListView | |
# We will have access to object_list which is usually the items in the query_set | |
class ViewListPost(ListView): | |
queryset = ModelPost.objects.all() | |
# model = ModelPost | |
# paginate_by = 2 | |
template_name = 'db_crud/list.html' | |
class ViewCreatePost(CreateView): | |
template_name = 'db_crud/create.html' | |
# form_class = FormPost | |
model = ModelPost | |
fields = '__all__' # or [] | |
def form_valid(self, form): | |
# Don't call super(..) if you want to process the model further(add timestamp, and other fields, etc) | |
# super(ViewCreatePost, self).form_valid(form) | |
model = form.save(commit=False) | |
model.save() | |
return HttpResponseRedirect(reverse('demos-models-dbcrud-list')) | |
# You have to either return an HttpResponse(or subclasses), or define get_absolute_url, success_url, etc | |
class ViewUpdatePost(UpdateView): | |
model = ModelPost | |
template_name = 'db_crud/update.html' | |
fields = ['title', 'body'] | |
def get_object(self, queryset=None): | |
id = self.kwargs['id'] | |
return self.model.objects.get(id=id) | |
def form_valid(self, form): | |
form.save() | |
return HttpResponseRedirect(reverse('demos-models-dbcrud-list')) | |
class ViewDetailPost(DetailView): | |
model = ModelPost | |
template_name = 'db_crud/detail.html' | |
# Display a confirmation warning before deleting, if triggered with GET: it shows the warning(template view) | |
# If triggered with POST then deletes, the template will receive object, which is the item to be deleted | |
class ViewDeletePost(DeleteView): | |
template_name = 'db_crud/delete.html' | |
model = ModelPost | |
# Notice get_success_url is defined here and not in the model, because the model will be deleted | |
def get_success_url(self): | |
return reverse('demos-models-dbcrud-list') | |
####################### LIST.HTML ########################## | |
{% extends 'base.html' %} | |
{% block content %} | |
<a href="{% url 'demos-models-dbcrud-create' %}">Create New</a> | |
<br> | |
{% if object_list %} | |
{% for post in object_list %} | |
<span>{{ post.title }}</span> | |
<p>{{ post.body }}</p> | |
<a href="{% url 'demos-models-dbcrud-detail' post.id %}" class="btn btn-success btn-sm"> | |
<span class="glyphicon glyphicon-edit"></span> | |
Read more</a> | |
<a href="{% url 'demos-models-dbcrud-update' post.id %}" class="btn btn-warning btn-sm"> | |
<i class="fa fa-pencil" aria-hidden="true" style="color: black"></i> | |
Edit</a> | |
<a href="{% url 'demos-models-dbcrud-delete' post.id %}" class="btn btn-danger btn-sm"> | |
<i class="fa fa-trash" aria-hidden="true" style="color: black"></i> | |
Delete | |
</a> | |
<hr> | |
{% endfor %} | |
{% endif %} | |
{% endblock %} | |
####################### CREATE.HTML ########################## | |
{% extends 'base_bak.html' %} | |
{% block content %} | |
<a href="{% url 'demos-models-dbcrud-list' %}">Go Back To list</a> | |
<form action="" method="post">{% csrf_token %} | |
{{ form.as_p }} | |
<button type="submit" class="btn btn-info">Create</button> | |
</form> | |
{% endblock %} | |
####################### UPDATE.HTML ########################## | |
{% extends 'base_bak.html' %} | |
{% block content %} | |
<a href="{% url 'demos-models-dbcrud-list' %}">Go Back To list</a> | |
<form action="" method="post">{% csrf_token %} | |
{{ form.as_p }} | |
<button type="submit" class="btn btn-info">Update</button> | |
</form> | |
{% endblock %} | |
####################### DELETE.HTML ########################## | |
{% extends 'base_bak.html' %} | |
{% block content %} | |
<a href="{% url 'demos-models-dbcrud-list' %}">Go Back To list</a> | |
<form method="post"> | |
{% csrf_token %} | |
<button class="btn btn-danger">Are you sure you want to delete {{ object }}?</button> | |
</form> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment