How we can implement CRUD functionality using generic views and django-mptt ?? I've searched a lot and couldn't find a single tutorial/sample code.
Let's say we have a Course hierarchy or Category hierarchy, or similar thing ... How we can Add/Delete/Update/Read them ??
For instance I have this model:
from django.db import models
from mptt.models import MPTTModel , TreeForeignKey
class Genre(MPTTModel):
name = models.CharField(max_length = 50 , unique = True)
parent = TreeForeignKey('self' , null = True , blank = True , related_name = 'children')
class MPTTMeta:
order_insertion_by = ['name']
and this views.py:
from django.views.generic.list_detail import object_list
from mp.models import Genre
def genres_list(request):
''' Shows all of the genres '''
return object_list(request,
queryset = Genre.tree.all() ,
template_name = 'genres.html' ,
# template_object_name = 'nodes' ## Adding "nodes" variable didn't solve the problem
)
well ... I get this error (error is in line number "5" : {% recursetree nodes %}):
Caught VariableDoesNotExist while rendering: Failed lookup for key [nodes] in u"[{'paginator': None, 'is_paginated': False, 'page_obj': None, 'nodes_list': [<Genre: Genre object>, <Genre: Genre object>, <Genre: Genre object>, <Genre: Genre object>]}, {'csrf_token': <django.utils.functional.__proxy__ object at 0x7f5bb810f090>}, {'perms': <django.utils.functional.__proxy__ object at 0x7f5bb810ff10>, 'messages': <django.contrib.messages.storage.user_messages.LegacyFallbackStorage object at 0x324af50>, 'user': ....................................
<html>
2
3 {% load mptt_tags %}
4 <ul>
5 {% recursetree nodes %}
6 <li>
7 {{node.name}}
Simple CRUD application with MPTT models and class-based generic views (Django 1.4 The function-based implementation has been deprecated).
Let's begin
urls.py
from django.conf.urls.defaults import patterns, include, url
from django.views.generic import DetailView, ListView, CreateView, UpdateView
from genre.models import Genre
urlpatterns = patterns('',
url(r'detail/(?P<pk>\d+)', DetailView.as_view(model=Genre), name="genre_detail",),
url(r'update/(?P<pk>\d+)', UpdateView.as_view(model=Genre), name="genre_update",),
url(r'create', CreateView.as_view(model=Genre), name="genre_create",),
url(r'list', ListView.as_view(model=Genre), name="genre_list",),
)
models.py
from django.core.urlresolvers import reverse
from django.db import models
from mptt.models import MPTTModel
class Genre(MPTTModel):
name = models.CharField(max_length=50 , unique=True)
parent = models.ForeignKey('self' , null=True , blank=True , related_name='children')
def get_absolute_url(self):
return reverse('genre_detail', kwargs={'pk': self.pk, })
class MPTTMeta:
order_insertion_by = ['name']
templates/genre_detail.html
<html>
<body>
<div>Object: {{ object }}</div>
<div>Object's name: {{ object.name }}</div>
<div>Object's parent: {{ object.parent }}</div>
</body>
</html>
templates/genre_form.html
<html>
<body>
<form action="" method="post">
{% csrf_token %}
{{ form.as_ul }}
<button>save</button>
</form>
</body>
</html>
templates/genre_list.html
{% load mptt_tags %}
<html>
<body>
<ul class="root">
{% recursetree object_list %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
</body>
</html>
and that's it.
I had some spare time today and shared this project on github https://github.com/kaygorodov/simple-crud-mptt.
How can I define my own class-based view?
genre/views.py
from django.view.generic import UpdateView
class MyCustomUpdateView(UpdateView):
model = Genre
def get_form_kwargs(self):
"""
Returns the keyword arguments for instanciating the form.
"""
kwargs = super(MyCustomUpdateView, self).get_form_kwargs()
kwargs.update({'my_first_param_to_init_form': 1,
'my_second_param_to_init_form': 2,
})
return kwargs
genre/urls.py
url(r'update/(?P<pk>\d+)', MyCustomUpdateView.as_view(), name="genre_update",),
Related
I'm writing a code in django framework for a sales website and the number of available pieces of a certain merchandise is limited.
to obtain the number of remaining products I have to call a certain function.
Now I wanted to ask if there's any way to call this function in the models.py or forms.py modules or any other way to set this limit.
This is my view module:
from django.http.response import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from .forms import orderForm
from regpage.models import User
from django.views import View
class orderView(View):
def get(self, request):
form = orderForm()
return render(request, "dashboard/order.html", {'form': form,})
This is my forms module:
from django import forms
from .models import Order
class orderForm(forms.ModelForm):
class Meta:
model = Order
fields = ['productCount']
This is my models module:
from django.db import models
from django.db.models.enums import Choices
from regpage.models import User
from django.core import validators
from django.core.validators import EmailValidator, MaxValueValidator, MinValueValidator
class Order(models.Model):
userID = models.ForeignKey(User, on_delete=models.CASCADE)
productCount = models.PositiveSmallIntegerField() #THE LIMITED FIELD
This is my html file:
{% extends 'dashboard.html' %}
{% block dashTitle %}
Order
{% endblock dashTitle %}
{% block dashContent %}
<p style="text-align:center;font-size:30px;"><b> Order Page </b> </p>
<form style="text-align:center;font-size:25px" method="POST">
{% csrf_token %}
{% for field in form %}
<div class="form-control {% if field.errors %}errors{% endif %}">
{{ field.label_tag }}
{{ field }}
{{ field.errors }}
</div>
<br>
{% endfor %}
<br>
<button type="submit">Order</button>
</form>
{% endblock dashContent %}
There's only one product in this website and one function should be called to obtain the number of remaining products.
Give this a try
class orderForm(forms.ModelForm):
quantity = forms.IntegerField(widget=forms.TextInput(
attrs={
'min':0,
'value':0,
'type':'number',
}))
class Meta:
model = Order
fields = ('__all__')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['productCount'].widget.attrs.update(
{'max': self.instance.productCount},
)
I am trying to implement basic search functionality with Django. Could use help with accessing query inputs from forms in templates in functional or class based views. Intended functionality:
If exact search query page exists, display that page
If there is a page title that contains what was queried, show results of all
If neither, display a message stating no results found
I'm just learning, so I tried functional views and class based views. I've spent a really long time on documentation/videos/textbooks and don't see how to get the intended behavior out of class-based view. I understand collecting object_list and getting query_set, but how do you then route to those three different conditions. I tried overriding dispatch() and as_views() method to no avail. Tried with a Django form class and without.
For some reason, the functional view keeps executing the first try statement instead of throwing a DoesNotExist exception when the exact match isn't found. So it shows the entry page instead of the search results page. It seems like the request.GET is None type no matter what, as when I try to print nothing shows up.
urls.py
from re import search
from django.urls import path
from . import views
from .views import IndexPageView, SearchView
app_name = "wiki"
urlpatterns = [
# ex: /wiki/
path("", views.index, name="index"),
# path("", IndexPageView.as_view(), name="index"),
# ex: /wiki/EntryPageName
path("wiki/<str:entry>/", views.displayEntry, name="displayEntry"),
# path("wiki/search/", views.searchView, name="searchView")
path("wiki/search/", SearchView.as_view(), name="searchView")
]
model
class Entry(models.Model):
title = models.CharField(max_length=64)
def __str__(self):
return f"{self.title}"
content = models.TextField()
views.py
def searchView(request):
searchedTerm = request.GET.get('q')
try:
exactMatch = Entry.objects.get(title=searchedTerm)
entryTitle = exactMatch.title
entryHTML = markdown2.markdown(exactMatch.content)
return render(request, "encyclopedia/displayEntry.html", {
"entryTitle": entryTitle,
"entryHTML": entryHTML
})
except:
try:
searchResults = Entry.objects.filter(Q(title__icontains=searchedTerm))
return render(request, "encyclopedia/searchResults.html", {
"searchResults": searchResults,
"searchedTerm": searchedTerm
})
except:
return render(request, "encyclopedia/searchResults.html", {
"emptyResults": f"No entries found matching: {searchedTerm}",
"searchedTerm": searchedTerm
})
class SearchView(ListView):
template_name = "encyclopedia/searchResults.html"
model = Entry
context_object_name = "searchList"
def get_queryset(self):
searchedTerm = self.request.GET.get('q')
try:
searchResults = Entry.objects.get(title=searchedTerm)
return searchResults
except:
try:
searchResults = Entry.objects.filter(Q(title__icontains=searchedTerm))
return searchResults
except:
pass
def as_view():
searchedTerm = self.request.GET.get('q')
try:
exactMatch = Entry.objects.get(title=searchedTerm)
entryTitle = exactMatch.title
entryHTML = markdown2.markdown(exactMatch.content)
return render(request, "encyclopedia/displayEntry.html", {
"entryTitle": entryTitle,
"entryHTML": entryHTML,
})
except:
searchResults = Entry.objects.filter(Q(title__icontains=searchedTerm))
return render(request, "encyclopedia/searchResults.html", {
"searchResults": searchResults,
"searchedTerm": searchedTerm
})
else:
return render(request, "encyclopedia/searchResults.html", {
"emptyResults": f"No entries found matching: {searchedTerm}",
"searchedTerm": searchedTerm
})
search form from layout.html
<form action="{% url 'wiki:search' %}" method="GET">
<input class="search" type="text" name="q" placeholder="Search Encyclopedia">
<!-- <input type="submit" value="submit"> -->
</form>
display entry page template
{% extends "encyclopedia/layout.html" %}
{% block title %}
{% if entryTitle %}
{{ entryTitle }}
{% else %}
Page Not Found!
{% endif %}
{% endblock %}
{% block body %}
{% if entryHTML %}
{{ entryHTML|safe }}
{% else %}
<p>This page does not exist yet.</p>
<p>Check your spelling or create a new entry!</p>
<p>?? {{ testPrint }}</p>
{% endif %}
{% endblock %}
search results page
{% extends "encyclopedia/layout.html" %}
{% block title %}
Search Results: {{ searchedTerm }}
{% endblock %}
{% block body %}
{% if searchResults %}
<h3>Search Results</h3>
<ul>
{% for result in searchResults %}
<li>{{ result.title }}</li>
{% endfor %}
</ul>
{% else %}
<h3>{{ emptyResults }}</h3>
{% endif %}
{% endblock %}
Models
class Entry(models.Model):
title = models.CharField(max_length=64)
content = models.TextField()
def __str__(self):
return self.title
Views
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from app.models import Entry
class EntryListView(ListView):
model = Entry
paginate_by = 100 # if pagination is desired
def get_queryset(self):
queryset = super().get_queryset()
q = self.request.GET.get("q")
if q:
queryset = queryset.filter(title__icontains=q)
return queryset
class EntryDetailView(DetailView):
model = Entry
Urls
from django.urls import path
from app.views import EntryListView, EntryDetailView
urlpatterns = [
path('', EntryListView.as_view(), name='entry-list'),
path('<int:pk>/', ArticleDetailView.as_view(), name='entry-detail'),
]
I've followed the tutorial here to implement a basic search function: https://learndjango.com/tutorials/django-search-tutorial
I'd like to extend that tutorial by making the search function visible on the results page, allowing for repeated search. However, when I do this I can't get the search form to show up on the search results page. The search button shows up, but not the field to provide input.
Relevant code:
home.html:
<div name="searchform">
<form action="{% url 'search_results' %}" method="get">
{{ form }}
<input type="submit" value="Search">
</form>
</div>
{% block content %}
{% endblock %}
search_results.html:
{% extends home.html}
{% block content %}
<h1>Search Results</h1>
<ul>
{% for city in object_list %}
<li>
{{ city.name }}, {{ city.state }}
</li>
{% endfor %}
</ul>
{% endblock %}
Views.py:
from django.db.models import Q
from django.views.generic import TemplateView, ListView, FormView
from .models import City
class HomePageView(FormView):
template_name = 'home.html'
form_class = SearchForm
class SearchResultsView(ListView):
model = City
template_name = 'search_results.html'
def get_queryset(self):
query = self.request.GET.get('q')
object_list = City.objects.filter(
Q(name__icontains=query) | Q(state__icontains=query)
)
return object_list
urls.py:
from django.urls import path
from .views import HomePageView, SearchResultsView
urlpatterns = [
path('search/', SearchResultsView.as_view(), name='search_results'),
path('', HomePageView.as_view(), name='home'),
]
forms.py:
from django import forms
class SearchForm(forms.Form):
q = forms.CharField(label='', max_length=50,
widget=forms.TextInput(attrs={'placeholder': 'Search Here'})
)
Any advice on how I might troubleshoot this sort of issue (or if I'm blatantly doing something un-django-y) would be greatly appreciated.
You're using ListView which is a Generic display view.
You need to use get method, then you can pass the form to make the search again and stay on the same page.
class SearchResultsView(View):
template_name = 'search_results.html'
form_class = SearchForm
def get(self, request):
form = self.form_class()
query = self.request.GET.get('q')
context = {}
context['form'] = form
context['cities'] = City.objects.filter(
Q(name__icontains=query) | Q(state__icontains=query)
)
return render(self.request, self.template_name, context)
You can achieve the same result with ListView but is better if you use other based view class.
You can check the doc. here
class HomePageView(FormView):
template_name = 'home.html'
form_class = SearchForm # This line!
Remember to also apply the form_class attribute to SearchResultsView, otherwise, no forms will be interpreted. The submit button only shows up because it's not a part of the rendered form.
I am creating a simple form that takes in basic info of school. I created it using 'CreateView' CBV but when I hit the URL, I get the value error: Value error: Field 'id' expected a number but got 'create'.
error page
Model.py:
from django.db import models
class School(models.Model):
name=models.CharField(max_length=256)
principal = models.CharField(max_length=256)
location = models.CharField(max_length=256)
def __str__ (self):
return self.name
def get_absolute_url(self):
return reverse("basic_app:detail",kwargs={'pk':self.pk})
class Student(models.Model):
name=models.CharField(max_length=256)
age=models.PositiveIntegerField()
school = models.ForeignKey(School,related_name='students',on_delete=models.CASCADE)
def __str__(self):
return self.name
Views.py
from django.views.generic import View, CreateView
from . import models
class SchoolCreateView(CreateView):
fields=["name","principal","location"]
model=models.School
urls.py:
from django.urls import path
from basic_app import views
app_name= 'basic_app'
urlpatterns=[
path('create/',views.SchoolCreateView.as_view(),name='create')
]
school_form.html:
{% block body_block %}
<h1>
{% if not form.instance.pk %}
Create school:
{% else %}
Update school:
{% endif %}
</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class = "btn btn-primary" value="Submit">
</form>
{% endblock %}
Note:
The URL works if I replace the URL path like 'create/school' (anything added after 'create/)
path('create/school',views.SchoolCreateView.as_view(),name='create')
then the page gets loaded. I could not figure out the issue, can anyone point out the mistake with the previous URL path.
I'm new in Django. There is a html page (project_details) which should show the title and the tasks of the project, but shows only the title of the project, not the tasks. The tasks exists, the problem is the filter!!!
views.py The error is here
from .models import Project,Task
from django.views.generic import ListView, DetailView
class ProjectsList(ListView):
template_name = 'projects_list.html'
queryset= Project.objects.all()
class ProjectDetail(DetailView):
model = Project
template_name = 'projects_details.html'
def get_context_data(self, **kwargs):
context = super(ProjectDetail, self).get_context_data(**kwargs)
## the context is a list of the tasks of the Project##
##THIS IS THE ERROR##
context['tasks'] = Task.object.filter(list=Project) <---->HERE ((work with Task.object.all() ))
return context
models.py
class Project(models.Model):
title = models.CharField(max_length=30)
slug = AutoSlugField(populate_from='title', editable=False, always_update=True)
class Task(models.Model):
title = models.CharField(max_length=250)
list = models.ForeignKey(Project)
slug = AutoSlugField(populate_from='title', editable=False, always_update=True)
urls.py
from django.conf.urls import url
from .models import Project
from .views import ProjectsList, ProjectDetail
urlpatterns = [
url(r'^$', ProjectsList.as_view(), name='project_list'),
url(r'(?P<slug>[\w-]+)/$',ProjectDetail.as_view() , name='project_details'),]
projects_details.html
{% extends './base.html' %}
{% block content %}
<div>
<a href={{ object.get_absolute_url }}>
<h4> {{object.title}} </h4>
</a>
<ul>
{% for task in tasks %} <----> NO OUTPUT <li>
<li> {{task}}</li>
{% endfor %}
</ul>
</div>
{% endblock content %}
Sorry for my bad English.
Project is the model class, so doing (list=Project) doesn't make sense.
If you want to access the object in the detail view's get_context_data method, you can use self.object:
def get_context_data(self, **kwargs):
context = super(ProjectDetail, self).get_context_data(**kwargs)
context['tasks'] = Task.objects.filter(list=self.object)
return context
However, you don't actually have to override the get_context_data method at all. In your template, you can follow the relationship backwards from a project to get its tasks:
{% for task in object.task_set.all %}
<li>{{task}}</li>
{% endfor %}