Pass model from url to create_object generic input view - django

I have multiple models that I want to create generic inputs for. My first pass used two separate urls:
url(r'^create_actor/$, create_object, {'model': Actor, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Actor'}, 'login_required': 'True'}),
url(r'^create_movie/$, create_object, {'model': Movie, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Movie'}, 'login_required': 'True'}),
I assume it would be much better to combine these into one statement. I'm not sure how to pass a variable from the url into the parameters such that the line would dynamically select the model based on the variable.

I haven't tried this, but you can use a variable to capture the value after create_ and have it automatically sent to the create_object view:
url(r'url(r'^create_(?P<model>\w+)/$, create_object, {'template_name': 'create.html', 'post_save_redirect': '/library/', 'login_required': 'True'})
You'll have to access this url as /create_Actor/ instead of /creat_actor/. I'm not sure how to get the extra_context key to work.
Hope this helps.

from django.db import models
url(r'^create_(?P<modelname>\w+)/$', generic_add),
def generic_add(request, modelname):
mdlnm_model = models.get_model('catalog',modelname)
return create_object(request,
model = mdlnm_model,
template_name = 'create.html',
post_save_redirect = '/library/',
login_required = 'True'
)

Related

Publish a custom Django Flatpage at a set date and time

I have a custom Flatpage model:
from django.contrib.flatpages.models import FlatPage
class MyFlatPage(FlatPage):
publish = models.DateTimeField()
so that I can add a publish date in the future.
Now, I don't have a proper list of flatpages on the front end, my use for frontpages is more like 'one-offs', where I specific the URL and all that. For example, 'about', '2019prize', 'Today's walk', stuff like that.
The urls.py is set up to catch all the flatpages with:
from django.contrib.flatpages import views
re_path(r'^(?P<url>.*/)$', views.flatpage)
How can I set these pages I create to be displayed only after the publish date has arrived? I know that I can filter them by looking up something like pages.filter(publish__lte=now). Where and how should I put that code though?
Additional information
I suppose I need to create a custom view, is that correct? The original view is in ../lib/python3.8/site-packages/django/contrib/flatpages/views.py:
def flatpage(request, url)
if not url.startswith('/'):
url = '/' + url
site_id = get_current_site(request).id
try:
f = get_object_or_404(FlatPage, url=url, sites=site_id)
except Http404:
if not url.endswith('/') and settings.APPEND_SLASH:
url += '/'
f = get_object_or_404(FlatPage, url=url, sites=site_id)
return HttpResponsePermanentRedirect('%s/' % request.path)
else:
raise
return render_flatpage(request, f)
#csrf_protect
def render_flatpage(request, f):
if f.registration_required and not request.user.is_authenticated:
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(request.path)
if f.template_name:
template = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
else:
template = loader.get_template(DEFAULT_TEMPLATE)
f.title = mark_safe(f.title)
f.content = mark_safe(f.content)
return HttpResponse(template.render({'flatpage': f}, request))
How can I extend this, adding my if publish__lte=now code?
What I did is copy-paste the view code from ../lib/python3.8/site-packages/django/contrib/flatpages/views.py to my app.views, rename the two functions, and add the following to render_myflatpage:
def render_myflatpage(request, f):
[...]
if f.publish > now:
f.content = 'This content will be published on ' + str(f.publish)
I then assigned the new view in the catch-all urls.py code:
re_path(r'^(?P<url>.*/)$', myflatpage)
I know this goes against the DRY protocol; this works for me for the time being. If there's a more elegant solution please do let me know.

How to perform a query by using URL with question mark in Django?

It seems like the original URL querying function has been removed from Django 3.1. Does anyone know how to do it with a new package?
The url.py:
urlpatterns = [
re_path(r'^portfolio/(?P<title>[\w-]+)/$' , BlogApp_View.displayPortfolio, name='displayPortfolio'),
path('portfolio/', BlogApp_View.selectPortfolio, name='selectPortfolio'),]
The view.py
def displayPortfolio(request):
title = request.GET.get('title')
portfolio = Article.objects.filter(articleType__name__contains = "Portfolio", title=title)
print(title)
DICT = {}
return render(request, 'Article/', DICT)
The problem is now if I visit http://127.0.0.1:8000/Blog/portfolio/?title=A_UAV_Positioning_Approach_Using_LoRa/, it will skip the re_path shows in url.py.
Instead, it goes to the path one.
I have tried str:title method but that is actually not what I want. I prefer using the question mark pattern to finish the query.
The part after the questionmark is the querystring [wiki] and is not part of the path. This thus means that regardless what patterns you write, you can not distinguish on this, since the path patterns, regardless whether it is a path or re_path, are never matched against a URL with a query string.
You thus should write a single view, and inspect the request.GET query dict (which is a dictionary-like representation of the query string and see if it contains a value for title.
Your urlpatterns thus look like:
urlpatterns = [
path('portfolio/', BlogApp_View.selectPortfolio, name='selectPortfolio'),
]
and in the view, you can see if it contains a title:
def selectPortfolio(request):
if 'title' in request.GET:
# contains a ?title=…
title = request.GET.get('title')
portfolio = Article.objects.filter(
articleType__name__contains='Portfolio',
title=title
)
data = {'portfolio': portfolio}
return render(request, 'some_template.html', data)
else:
# contains no ?title=…
# …
return …

Page view refers to id, whil path is not asking for one

I want to load a default django page. Nothing fancy. However, the error I get, hints at an id that is incorrectly set.
"Field 'id' expected a number but got 'zoekboek'."
The confusing things here (I am a django beginner, so I wouldn't be surprised if this is not confusing at all for you):
the path for this page in the urls.py is not asking for an id.
the view is not querying anything yet (I found some posts that had similar errors,
but related to a filter).
the debug info points to another view that indeed is requesting an id.
when I add a slash at the beginning of the path, the error is gone!
The code
urls.py
urlpatterns = [
path('', views.scholen, name='scholen'),
path('<school_id>', views.school_detail, name='school_detail'),
path('<school_id>/<groep_id>', views.school_groep, name='school_groep'),
path('<school_id>/<groep_id>/<UserProfile_id>', views.leerling_page, name='leerling_page'),
path('zoekboek', views.zoekboek, name='zoekboek'),
]
views.py
from django.shortcuts import render, redirect, reverse, get_object_or_404
from books.models import Book, Rating
from .models import School, Groep
from profiles.models import UserProfile, Hobby, Sport
from django.contrib.auth.models import User
# Create your views here.
def scholen(request):
"""
Homepage for participating
schools.
"""
scholen = School.objects.all()
context = {
'scholen': scholen,
}
return render(request, 'schools/school_landing.html', context)
def school_detail(request, school_id):
"""
Details of individual schools.
"""
school = get_object_or_404(School, pk=school_id)
groep = Groep.objects.filter(school=school)
context = {
'school': school,
'groep': groep,
}
return render(request, 'schools/school_detail.html', context)
def school_groep(request, school_id, groep_id):
"""
Details of groep.
"""
school = get_object_or_404(School, pk=school_id)
groep = get_object_or_404(Groep, pk=groep_id)
a = groep.naam
kinderen = UserProfile.objects.filter(groep=a)
context = {
'school': school,
'groep': groep,
'kinderen': kinderen,
}
return render(request, 'schools/school_groep.html', context)
def leerling_page(request, school_id, groep_id, UserProfile_id):
"""
Personal page of school kids.
"""
profile = get_object_or_404(UserProfile, pk=UserProfile_id)
# If viewer is owner of page, viewer can edit
owner = False
if request.user == profile.user:
owner = True
context = {
'profile': profile,
'owner': owner,
}
return render(request, 'schools/leerling_page.html', context)
def zoekboek(request):
"""
Page for kids to search their favorite book
"""
context = {
}
return render(request, 'schools/zoek_boek.html', context)
Is this enough information?
Simple fix: move path('zoekboek', views.zoekboek, name='zoekboek'), from the last place to the second place in your urls.
Why?
Because Django URLs are resolved using regular expressions; the docs say here in point 3:
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.
Since your URL path path('<school_id>', views.school_detail, name='school_detail'), is very generic, it matches any string including the string zoekboek; so the request to zoekboek falls into the second line in your URL conf and gets routed to the view school_detail() and a school_id is expected for that view.
Suggestion: to make the URL handling easier and so you can order the URL paths however you like, you could change the URL a bit and add a prefix (for example school/) so that not any string matches the URL paths. For example, this schould work:
urlpatterns = [
path('', ...),
path('school/<school_id>', ...),
path('school/<school_id>/<groep_id>', ...),
path('school/<school_id>/<groep_id>/<UserProfile_id>', ...),
path('zoekboek', ...),
]

Include view in base template

I'm making a simple blog app and i want to have blog categories in the navbar which is in every page of the website.
do i have to pass a categories = Category.objects.all() object to EVERY view or is there any simple way to achieve this?
Yes, look at context processors:
myapp/context_processors.py
def categories(request):
return {'categories': Category.objects.all()}
And then add this to the TEMPLATE_CONTEXT_PROCESSORS setting:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"myapp.context_processors.categories",
)
There is simpler solution, you can use template context processor. Simply create in your app file named context_processors (name of that file doesn't matter, but good practice is to name it that way) and inside that file create any function that will take request argument and return dictionary with additional context variables. Example:
from .models import Category
def categories(request):
categories = Category.objects.all()
return {
'categories': categories,
}
Now simply add your context processor into settings:
TEMPLATE_CONTEXT_PROCESSORS += (
'yourapp.context_processors.categories',
)

Django Date-Based Generic Views: How to Access Variables

I have a series of urls tied to Django's generic date views. In the extra_context parameter, I'd like to pass in a queryset based off the year/ month variables in the URLs, but I'm not sure how to access them. For example, in
url(r'^archive/(?P<year>20[1-2][0-9])/?$', archive_year,
{'queryset': Article.objects.all(),
'date_field': 'publication_date',
'template_name': 'articles/archive-date-list.html',
'extra_context': {'content': 'articles'}},
name='article_archive'),
I'd like to add in the 5 most recent articles where the publication date's year is gte year and lt year + 1. Ideally, the collection would be looked up on each request and not just cached at compile time. Am I better off writing a context processor for this/ extending the view?
You create a wrapper around the generic view:
# myapp/views.py
def my_archive_year(request, year):
# Logic to get the articles here
return archive_year(request,
year=year,
date_field='publication_date',
template_name='articles/archive-date-list.html',
extra_context = {'content': articles}
)
# urls.py
url(r'^archive/(?P<year>20[1-2][0-9])/?$', 'myapp.views.my_archive_year', name='article_archive'),