custom url patterns in Django - django

I have a website I am trying to build for personal use, and it possesses two id's one for a meeting (where the race is run) and one for the event (the race number). The event id is in the form of "123456_01" and is passed in the model as a primary key for the Event model, as seen below...
class Event(models.Model):
meeting = models.CharField(max_length=500)
meetingID = models.ForeignKey(Meeting, on_delete='CASCADE', related_name='races')
eventID = models.CharField(max_length=300, primary_key=True)
venue = models.CharField(max_length=600, null=True)
race_no = models.CharField(max_length=2)
event_time = models.TimeField()
status = models.CharField(max_length=100)
distance = models.CharField(max_length=600)
I currently have the views file set up as follows:
class EventDetailView(DetailView,LoginRequiredMixin):
context_object_name = 'race_detail'
template_name = 'event.html'
model = models.Event
slug_url_kwarg = 'eventID'
I also have my front end set up so that at present when I click on a certain race, it automatically navigates to the page with the link http://127.0.0.1:8000/app/123456_01/, so that part is working through this config in the HTML:
{% url 'bettingUI:race' eventID=events.eventID %}
the problem I seem to be having is with the configuration of the urls.py file and possibly something I am missing in the views.py file.
my urls.py file is set up as follows :
from django.urls import path, include
from . import views
app_name = 'bettingUI'
urlpatterns = [
path('',views.DashListView.as_view(),name='dashboard'),
path('<eventID>/', views.EventDetailView.as_view(), name='race'),
]
I thought from reading the docs that I need to use a slug because of the '_' character in the ID I am passing in but I am constantly getting an error in the browser stating that it can not resolve keyword 'slug' into the field. Choices are: dro_eventID, dro_meetingID, dro_meetingID_id, event_time, meeting, race_no, runners, status, venue ( **the fields of the model). If I change the urls.py file to the below, I get the same error:
path('<slug:eventID>/', views.EventDetailView.as_view(), name='race'),
I am a bit lost here so would love some guidance.
Thank you.
I worked it out, the answer is to input <slug:pk>
but now I am getting an error at my dashpage (the page i land at to click through to the race page):
NoReverseMatch at /app/
Reverse for 'race' with keyword arguments '{'eventID': '1216859_01'}' not found. 1 pattern(s) tried: ['app/(?P<pk>[-a-zA-Z0-9_]+)/$']

So I give it again now the working version:
First you should add a slug field to your Event Model and this will let you use slug, so your model will look like this:
from django.utils.text import slugify
class Event(models.Model):
meeting = models.CharField(max_length=500)
meetingID = models.ForeignKey(Meeting, on_delete='CASCADE', related_name='races')
eventID = models.CharField(max_length=300, primary_key=True)
venue = models.CharField(max_length=600, null=True)
race_no = models.CharField(max_length=2)
event_time = models.TimeField(null=True)
status = models.CharField(max_length=100, null=True)
distance = models.CharField(max_length=600, null=True)
slug = models.SlugField(max_length=50, null=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.eventID, allow_unicode=True)
return super(Event, self).save(*args, **kwargs)
Notice the save() function and in that we added a slugify() method to slugify the eventID field at event savings.
Then your views should look like these:
from .models import Event, Meeting
class EventList(ListView):
model = Event
template_name = 'event_list.html'
context_object_name = 'race_list'
class EventDetailView(DetailView,LoginRequiredMixin):
context_object_name = 'race_detail'
template_name = 'myusers1/event.html' # this could be only event.html if the template is in yourapp/templates/ folder directly
model = Event
slug_url_kwarg = 'slug'
Notice in the above view that we now use actually the default slug definition.
I put the listview url under races/ sub-url but you can put it anywhere you want. And in your urls.py you can now use the slug values correctly like:
path('races/<slug:slug>/', views.EventDetailView.as_view(), name='race'),
path('races/', views.EventList.as_view(), name='race_list'),
In my trial app the templates look like the followings: listview template:
{% extends 'myusers1/base.html' %}
{% block content %}
<div class"container">
<div class="col col-lg-2">
<h2>Races</h2>
<ul>
{% for race in race_list %}
<div class="col-xs-12 .col-md-8"><li> {{ race.venue }} </li></div>
{% endfor %}
</ul>
</div>
</div>
{% endblock %}
And the detail template looks like this:
{% extends 'myusers1/base.html' %}
{% block content %}
<div class"container">
<div class="col col-lg-2">
<h2>Race Details</h2>
<div class="col-xs-12 .col-md-8"> <h4>Venue name: </h4> {{ race_detail.venue}} </div>
<div class="col-xs-12 .col-md-8"> <h4>Event ID: </h4> {{ race_detail.eventID }} </div>
<div class="col-xs-12 .col-md-8"> <h4>Meeting name: </h4> {{ race_detail.meeting }} </div>
<div class="col-xs-12 .col-md-8"> <h4>Meeting ID: </h4> {{ race_detail.meetingID.id }} </div>
</div>
</div>
{% endblock %}
And the visual result about how dynamic urls work using the above:
I hope that the above will help you to finalize your app list and details view now. Cheers.

I think I found a solution here try this:
url.py:
path('<slug:eventID>/', views.EventDetailView.as_view(), name='race')
Now you can simple get the instance of Event in your EventDetailView generic view by using get_object method like this:
class EventDetailView(DetailView, LoginRequiredMixin):
context_object_name = 'race_detail'
template_name = 'event.html'
model = models.Event
def get_object(self):
e1 = Event.objects.get(eventID=self.kwargs['eventID'])
print (e1.eventID) # or e1.pk gives: 123456_01
return e1
You can also change your eventID from CharField to SlugField. And still have it working.

Related

How can I gather all objects, from all models, within one class based view in django 2.2

A friend of mine is trying to gather all of the data from each of his models within one view, to display on one tamplate, with the 'slug' as the URL.
He currently has a class based view that looks like this:
from itertools import chain
class ProductDetailView(DetailView):
queryset1 = BooksProduct.objects.all()
queryset2 = ClothingProduct.objects.all()
queryset3 = FurnitureProduct.objects.all()
queryset4 = DecoratingProduct.objects.all()
queryset = chain(queryset1, queryset2, queryset3, queryset4)
template_name = 'products/detail.html'
The URL looks like this:
urlpatterns =[
path('product-detail/<slug>/', ProductDetailView.as_view(), name='product-detail'),
]
The four different models all look very similar to this:
class BookProduct(models.Model):
slug = models.SlugField(max_length=40)
image = models.ImageField(upload_to='media')
title = models.CharField(max_length=150)
description = models.TextField()
short_description = models.CharField(max_length=300)
price = models.DecimalField(max_digits=5, decimal_places=2)
stock_quantity = models.PositiveIntegerField()
in_stock = models.BooleanField(default=True)
main_category = models.CharField(choices=PRODUCT_CATEGORY_CHOICES, max_length=2, default='FW')
brand = models.CharField(choices=TOY_BRAND_CHOICES, max_length=2, default='NB')
on_sale = models.BooleanField(default=False)
date_added = models.DateTimeField(auto_now_add=True, blank=True)
def __str__(self):
return self.title
class Meta:
ordering = ['-date_added']
def get_absolute_url(self):
return reverse("products:product-detail", kwargs={
'slug': self.slug
})
At the moment, clicking on an individual product to get the product-detail.html view results in this error:
'itertools.chain' object has no attribute 'all'
How is he able to accumulate all of the products from each model into one view?
The DetailView has since been changed to ListView which has made the product details render through to the template, however as soon as you try to refresh the page, or press the back button and re click the product to view the detail page again, it disappears, until he resets the server over again.
The HTML template looks like this:
<section id="detail-view">
<div class="container">
<div class="row">
<div class="col-6">
<img src="{{ product.image.url }}" alt="{{ product.title }}">
</div>
<div class="col-6">
{% for product in object_list %}
<h1>{{ product.title }}</h1>
<strong>£{{ product.price }}</strong>
<hr class="newhr">
ADD TO BASKET
<div class="detail-desc-box">
<p>{{ product.description }}</p>
</div>
{% endfor %}
</div>
</div>
</div>
</section>
And the view now looks like this:
from itertools import chain
class ProductDetailView(ListView):
queryset1 = BooksProduct.objects.all()
queryset2 = ClothingProduct.objects.all()
queryset3 = FurnitureProduct.objects.all()
queryset4 = DecoratingProduct.objects.all()
queryset = chain(queryset1, queryset2, queryset3, queryset4)
template_name = 'products/detail.html'
Kind regards
I don't think you'll be able to use the Generic views out of the box like you're hoping. The reason is because the DetailView is specifically for showing a single model instance. You're attempting to show multiple.
What I'd recommend is to switch this to a ListView, supply an additional url keyword argument to the view to specify the slug, then do one of the following:
Use .union() to make one query to the database and deal with the constraints of that method detailed in the docs.
Refactor your models to use inheritance and make the select based on the common model.
Note:
The error you're encountering currently is because you're effectively changing the type of DetailView.queryset from QuerySet to a generator. The rest of the generic view expects certain members to be present, such as .all().

How to arrange posts from different models in Order of time when they were created

Intro: I have a 3 models user, post, group. User is able to make posts however each post has to belong to a group. I have 400 fixed groups. Users have to choose from the existing 400 groups for their posts. Users cannot add, delete, update group's.
Furthermore:
Users can become a member of groups and when they click on a certain group. They see all the posts in that group.
Users can follow-unfollow other users.
**What I have right now:**When a user signs-in. In his home page he sees. All the posts of the each individual group he is a member of. When all the posts from all the groups are done with. He then sees the posts of all the people he follows one by one
What I want: I want the posts to be arranged by time
Example: If one of the people he follows just wrote a post then that post is first. Chronologically the second post was in one of the groups that he is a member of. That post comes second... You get the idea
Below are my models
class Post(models.Model):
user = models.ForeignKey(User, related_name='posts')
group = models.ForeignKey(Group, related_name='posts')
title = models.CharField(max_length=250, unique=True)
message = models.TextField()
created_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-created_at']
class Group(models.Model):
name = models.CharField(max_length=250, unique=True)
members = models.ManyToManyField(User, through='GroupMember')
my views.py
class Homepage(TemplateView):
template_name = 'home.html'
def get_context_data(self, **kwargs):
context = super(Homepage, self).get_context_data(**kwargs)
posts_of_people_i_follow = []
posts_from_groups_that_i_member = []
if self.request.user.is_authenticated():
my = self.request.user
for user in my.following.all():
posts_of_people_i_follow += user.posts.all()
posts_of_people_i_follow.save()
for group in my.group_set.all():
posts_from_groups_that_i_member += group.posts.all()
posts_from_groups_that_i_member.save()
context['posts_of_people_I_follow_list'] = posts_of_people_i_follow
context['posts_from_groups_that_i_member'] = posts_from_groups_that_i_member
return context
In my Templates I currently have
{% for post in posts_from_groups_that_i_member %}
{{ post.title }}
{{ post.message }}
{% endfor %}
{% for post in posts_of_people_I_follow_list %}
{{ post.title }}
{{ post.message }}
{% endfor %}
Option 2: After breaking my head on this for a while I have been trying a different approach. But this looks really expensive
In the user profile model
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
def get_all_posts(self):
my = self.user
all_posts_for_this_user = []
for user in my.following.all():
all_posts_for_this_user += user.posts.all()
all_posts_for_this_user.save()
for group in my.group_set.all():
all_posts_for_this_user += group.posts.all()
all_posts_for_this_user.save()
return all_posts_for_this_user
Then in my templates
{% for post in user.profile.get_all_posts %}
{{ post.title }}
{{ post.message }}
{% endfor %}
Can anyone think of a better solution. Or is this ok
if I understand it correctly, you need post date in your Post model then you can order posts by Post.objects.ordey_by('post_date')
https://docs.djangoproject.com/en/2.1/ref/models/querysets/#django.db.models.query.QuerySet.order_by
Modify your view class by using an 'order_by()' method on the queryset that fetches your post objects and filters them according to when they were created (The 'created_at' field comes in handy here).
Add this to your 'Homepage' view class:
def get_queryset(self):
queryset_list = Post.objects.order_by('-created_at')
Further reading can be found here:
https://docs.djangoproject.com/en/2.1/ref/models/querysets/
EDIT
I think the template rendering issue deserves its own question?
In your template, try this:
{% for post in posts_from_groups_that_i_member %}
{% if post in posts_of_people_I_follow_list %}
{{ post.title }}
{{ post.message }}
{% endfor %}
{% endfor %}
In terms of performance, it may not be the best option but it should work. Let me know your findings.

Filter a Django form select element based on a previously selected element

Let's consider the following models
models.py
Class Brand(models.Model):
company_name = models.CharField(max_length=100)
class CarModel(models.Model):
brand = models.ForeignKey(Brand)
name = models.CharField(max_length=100)
Class FleetCars(models.Model):
model_car = models.Foreignkey(CarModel)
What is the best way to solve this problem in django?
Suppose a form (for insertions in FleetCars) consists of two select elements, like this:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<br />Brand:
<select>
<option value="Brand1">Brand1</option>
<option value="Brand2">Brand2</option>
</select>
<br />
<br />Model:
<select>
<option value="Model1_B1">Model1_B1</option>
<option value="Model1_B2">Model1_B2</option>
</select>
</body>
</html>
In this case, I want the options in the second select to depend on the value selected in the first. For example, if the user chose Brand1 for a Brand in the first select, the second select would be filtered with only cars whose Brand was Brand1, that is, only "Model1_B1".
Obs.
I saw many solutions with forms.ModelChoiceField, but only works with edit and since the user do not change the brand.
After hours and hours of research, without success, I decided to try to solve on my own. The solution that I found maybe don't be the best or the more elegant, but is working. (For download full Django project, click on this repo => https://github.com/Sidon/djfkf/.)
models.py
from django.db import models
class Brand(models.Model):
company_name = models.CharField(max_length=100)
def __str__(self):
return self.company_name
class Car(models.Model):
brand = models.ForeignKey(Brand)
name = models.CharField(max_length=100)
def brand_name(self):
return self.brand.company_name
def __str__(self):
return self.name
class Fleet(models.Model):
car = models.ForeignKey(Car)
description = models.CharField(max_length=100)
def car_name(self):
return self.car.name
def brand(self):
return self.car.brand.company_name
def __str__(self):
return self.description
The goal is to register cars on the fleet. The only fields that are will be recorded: Car (foreign key) and description. On the form, there will be one select element for brands that will work just only as a helper for to filter the car's combo box.
forms.py
import json
from django import forms
from .models import *
class RegCarForm(forms.ModelForm):
dcars = {}
list_cars = []
for car in Car.objects.all():
if car.brand.company_name in dcars:
dcars[car.brand.company_name].append(car.name)
else:
dcars[car.brand.company_name] = [car.name]
list_cars.append((car.name,car.name))
brands = [str(brand) for brand in Brand.objects.all()]
brand_select = forms.ChoiceField(choices=([(brand, brand) for brand in brands]))
car_select = forms.ChoiceField(choices=(list_cars))
brands = json.dumps(brands)
cars = json.dumps(dcars)
class Meta:
model = Fleet
fields = ('brand_select', 'car_select', 'description',)
RegCarForm is a form for register cars, there are three fields: brand_select, car_select, and description. In addition, I defined two JSON attributes: 1) a dictionary whose keys are brands (strings) and values are lists of respective's cars and 2) A list of strings that represent the brands. Those two attributes will work as helpers for JS functions.
views.py
from django.shortcuts import render
from .forms import RegCarForm
from .models import *
def regcar(request):
if request.method == 'POST':
car_form = RegCarForm(data=request.POST)
if car_form.is_valid():
cdata = car_form.cleaned_data.get
car_selected = Car.objects.filter(name=cdata('car_select'))
reg1 = Fleet(car_id=car_selected[0].id, description=cdata('description'))
reg1.save()
else:
print ('Invalid')
else:
car_form = RegCarForm()
return render(request, 'core/regcar.html', {'car_form': car_form})
The view is practically auto-explanatory. Assigns the Form to the car_form variable, render the template core/regcar.html and, after Post, make the validation of the form and save the data.
regcar.html (template django)
{% extends "base.html" %}
{% block head %}
{% endblock %}
{% block content %}
<h1>Registering cars on the fleet. <br />(Populate one drop down based on selection in another)</h1>
<p>Change the contents of drop down Car based on the selection in dropdown Brand, using Django-forms + Javascritp</p>
<div class="select-style">
<form action="." method="post">
{% csrf_token %}
{{ car_form.as_p }}
<p><input type="submit" value="Register a car"></p>
</form>
</div>
{% endblock %}
{% block js %}
{% include "js1.html" %}
{% endblock %}
The template only just renders the form and load the script JS. Nothing else.
Finally, the js script, that makes the hard work.
{% block js %}
<script language="javascript">
$('#id_brand_select').change(function() {populateCar(this)});
$('#id_description').addClass('descriptions');
cars = {{ car_form.cars | safe }}
brands = {{ car_form.brands | safe}};
populateBrand();
$("#id_car_select").empty();
$("#id_car_select").append('<option value="" disabled selected>First select a brand</option>');
function populateBrand() {
$('#id_brand_select').empty();
$("#id_brand_select").append('<option value="" disabled selected>Select your option</option>');
$.each(brands, function(v) {
$('#id_brand_select')
.append($("<option></option>")
.attr("value", brands[v])
.text(brands[v]));
});
}
function populateCar(event) {
brand = $("#id_brand_select option:selected").text();
$("#id_car_select").empty();
$("#id_car_select").append('<option value="" disabled selected>Select your option</option>');
for (let [b, bcars] of Object.entries(cars)) {
if (b == brand) {
//alert(b);
for (car in bcars) {
$('#id_car_select')
.append($("<option></option>")
.attr("value", bcars[car])
.text(bcars[car]));
}
}
}
}
</script>
{% endblock %}
When the document is loaded, this script assigns the change event of brand_select (combo for selection of brand) to the function poplulateCar, assign the form's JASON attributes (cars and brands) to a JS variables and call the populateBrand function.
Links:
Full project in Django:
https://github.com/Sidon/djfkf/
class Country(models.Model):
country_name=models.CharField(max_length=10, blank=True, null=True)
class State(models.Model):
state_name=models.CharField(max_length=10, blank=True, null=True)
class MyCustomModal(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True, blank=True)
state = models.ForeignKey(State, on_delete=models.CASCADE, null=True, blank=True)
Here is my Form
class MyCustomForm(forms.ModelForm):
class Meta:
model = MyCustomModal
fields = [
'country',
'state',
]
def __init__(self, *args, **kwargs):
super(MyCustomForm, self).__init__(*args, **kwargs)
self.fields['country'] = forms.ChoiceField(choices=[('1','india'),('2','US')])
self.fields['state'].queryset = State.objects.filter(pk=2)

Merging querysets from different models

I have 2 models in one app and 1 view. I'm currently pulling information from 1 model perfectly fine. However i wish to pull in another model from the same app and output them both to the same page.
The idea of the page is it being a a news hub so it's pulling through different types of news posts (from one model) and a different type of post which is from the other model.
I'm fairly new to Django so go easy! :) Anyway here is the code:
//VIEWS
def news_home(request):
page_context = details(request, path="news-hub", only_context=True)
recent_posts = NewsPost.objects.filter(live=True, case_study=False).order_by("-posted")[:5]
recent_posts_pages = Paginator(recent_posts, 100)
current_page = request.GET.get("page", 1)
this_page = recent_posts_pages.page(current_page)
notes = BriefingNote.objects.filter(live=True).order_by("-posted")
news_categories = NewsCategory.objects.all()
news_context = {
"recent_posts": this_page.object_list,
"news_categories": news_categories,
"pages": recent_posts_pages,
"note": notes,
}
context = dict(page_context)
context.update(news_context)
return render_to_response('news_hub_REDESIGN.html', context, context_instance=RequestContext(request))
//model 1
class BriefingNote(models.Model):
title = models.CharField(max_length=300)
thumbnail = models.ImageField(upload_to='images/briefing_notes', blank=True)
file = models.FileField(upload_to='files/briefing_notes')
live = models.BooleanField(help_text="The post will only show on the frontend if the 'live' box is checked")
categories = models.ManyToManyField("NewsCategory")
# Dates
posted = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u"%s" % self.title
// model 2
class NewsPost(models.Model):
title = models.CharField(max_length=400)
slug = models.SlugField(help_text="This will form the URL of the post")
summary = models.TextField(help_text="To be used on the listings pages. Any formatting here will be ignored on the listings page.")
post = models.TextField(blank=True)
#TO BE REMOVED????
thumbnail = models.ImageField(help_text="To be displayed on listings pages", upload_to="images/news", blank=True)
remove_thumbnail = models.BooleanField()
I'm outputting the content on the front end like so:
{% for post in recent_posts %}
<div class='news_first'>
<img class="news_thumb" src="/media/{% if post.article_type %}{{post.article_type.image}}{% endif %}{% if post.news_type %}{{post.news_type.image}}{% endif%}" alt="">
<h3><a href='{{post.get_absolute_url}}'>{% if post.article_type.title %}{{post.title}}{% endif %} <span>{{post.posted|date:"d/m/y"}}</span></a></h3>
<p class='news_summary'>
{% if post.thumbnail %}<a href='{{post.get_absolute_url}}'><img src='{% thumbnail post.thumbnail 120x100 crop upscale %}' alt='{{post.title}}' class='news_thumbnail'/></a>{% endif %}{{post.summary|striptags}} <a href='{{post.get_absolute_url}}'>Read full story »</a>
</p>
<div class='clearboth'></div>
</div>
{% endfor %}
I was thinking perhaps i could output them both within the same forloop however they need to ordered by -posted. So i though this could mess things up.
If you need anymore info please let me know.
Thanks in advance.
I've now solved the problem.
news_hub = list(chain(notes, recent_posts))
news_hub = sorted(
chain(notes, recent_posts),
key = attrgetter('posted'), reverse=True)[:10]

How do I hyperlink an item returned from a queryset automatically

I have a model
class Transaction(models.Model):
sender = models.ForeignKey(MyBankAccount, unique=False, related_name="transactions_sent")
receiver = models.ForeignKey(RecipientBankAccount, unique=False, related_name="transactions_received")
created = models.DateField('transfer request date')
amount = models.FloatField()
createdby=models.ForeignKey(User,null=True)
which I display using
<li>{{ item.sender }} {{ item.receiver }} {{ item.amount }}</li>
I d like item.sender to be automatically hyperlinked to
http://example.com/johnsmith/trans/ where johnsmith is the name of the user that creates the transaction (aka createdby). I already implemented the view of this url. I just need to link to it.
So, presumably your url.py looks something like:
urlpatterns = patterns(
'',
# Patterns:
url(r'^(?P<username>[\w-]+)/(?P<createdby>[\w-]+)/$', transaction, name="transaction"),
....
Your view is:
def transaction(request, username, createdby):
...
Then your template fragment is:
{{ item.sender }}
Or you could define the following in your Transaction class (the urls.py file must remain):
def get_absolute_url(self):
from django.core.urlresolvers import reverse
return reverse('transaction', kwargs={'username': self.sender, 'createdby': self.receiver})
And then your template would look like:
{{ item.sender }}
But geez, this is all Django 101.