Django problem with having slug in my url while using a class - django

First i was using a detailslist with a def which was working fine, but i wanted to add a widget to show similar post (post in same category) so i had to change my def to a class to put a def get_context_data(self, pk), but now it always say et_context_data() got an unexpected keyword argument 'object'
here is my url
url(r'^(?P<slug>[\w-]+)$', views.postdetails.as_view(), name="postdetails"),
my view
class postdetails(DetailView):
model = Post
template_name = 'blog/post.html'
def get_context_data(self,**kwargs):
context = super(postdetail, self).get_context_data(**kwargs)
cat_id = self.kwargs.get('pk', None)
category = get_object_or_404(Category, id=cat_id)
getcat = category.post_set.all().order_by("-date")
resultcat = random.sample(getcat,4)
context['similarpost'] = resultcat
return context
and my model if needed
class Post(models.Model):
title = models.CharField(max_length = 140, unique=True)
slug = models.SlugField(max_length=40, blank=True, unique=True)
image = models.ImageField(upload_to="media", blank=True)
body = RichTextField(config_name='default')
date = models.DateField()
category = models.ManyToManyField(Category)
def __str__(self):
return self.title
I changed the pk in def get_context_data(self,pk): to **kwargs
but i get name 'postdetail' is not defined
thanks a lot

Related

Django Form Validators on Comparing One Object Against Another

I am creating a Non Disclosure Agreement form that a user fills out after registering and logging in. I am using a custom signup form with AllAuth and pre-populating parts of the form. I pre-populate the first and last name into the top part of the form as shown below in first screen shot, but as a part of the digital signature I am setting up; I need to validate the typed signature field matches the name of the first_name and the last_name concatenated together per the second screen-shot. I know I need to setup a validator based on Django Form & Field Validations and I've tried several things but just can't get my mind rapped around it. Any help putting this together would be huge...thank you.
My Models
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="profile", verbose_name="user")
...
class NonDisclosure(Timestamp):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="nda", verbose_name="profile")
user_signature = models.CharField(max_length=250, verbose_name='Signature')
user_street = models.CharField(max_length=250, verbose_name='Street Address')
user_city = models.CharField(max_length=250, verbose_name='City')
user_state = models.CharField(max_length=2, verbose_name='State Initials')
user_zip = models.IntegerField(verbose_name='Zip Code')
phone = models.CharField(max_length=25, verbose_name='Phone Number')
cash_on_hand = models.CharField(max_length=250, verbose_name='Cash on Hand')
value_of_securities = models.CharField(max_length=250, verbose_name='Value of Securities')
equity_in_real_estate = models.CharField(max_length=250, verbose_name='Equity on Real Estate')
other = models.CharField(max_length=250, verbose_name='Other Assets')
#property
def username(self):
return self.profile.username
#property
def first_name(self):
return self.profile.first_name
#property
def last_name(self):
return self.profile.last_name
#property
def email(self):
return self.profile.email
class Meta:
verbose_name = 'Non Disclosure Agreement'
verbose_name_plural = 'Non Disclosure Agreements'
def __str__(self):
return "%s" % self.profile
def get_absolute_url(self):
return reverse('nda_detail', args=[str(self.id)])
My Views:
class NonDisclosureForm(BaseModelForm):
cash_on_hand = forms.CharField(required=False)
value_of_securities = forms.CharField(required=False)
equity_in_real_estate = forms.CharField(required=False)
other = forms.CharField(required=False)
class Meta:
model = NonDisclosure
fields = ['user_signature', 'user_street', 'user_city', 'user_state', 'user_zip', 'phone', 'cash_on_hand', 'value_of_securities', 'equity_in_real_estate', 'other']
class NdaCreate(CreateView):
form_class = NonDisclosureForm
template_name = 'nda/nda_form.html'
def form_valid(self, form):
form.instance.profile = Profile.objects.get(user=self.request.user)
form.instance.created_by = self.request.user
return super(NdaCreate, self).form_valid(form)
Firstly, you should subclass ModelForm, not BaseModelForm. Write a clean_<fieldname> method for your user_signature field, and make sure that the value is as expected. You can access self.instance.created_by to check.
class NonDisclosureForm(ModelForm):
...
class Meta:
model = NonDisclosure
fields = ['user_signature', ...]
def clean_user_signature(self):
user_signature = self.cleaned_data['user_signature']
expected_name = '%s %s' % (self.instance.created_by.first_name, self.instance.created_by.last_name)
if user_signature != expected_name:
raise forms.ValidationError('Signature does not match')
return user_signature
Then you need to update your view so that it sets instance.created_by. You can do this by overriding get_form_kwargs.
class NdaCreate(CreateView):
def get_form_kwargs(self):
kwargs = super(NdaCreate, self).get_form_kwargs()
kwargs['instance'] = NonDisclosure(created_by=self.request.user)
return kwargs

How do I limit the ListView slug to a number of choices in Django?

Here is the code
models.py
class Submission(models.Model):
CAR = 'car'
TRUCK = 'truck'
VAN = 'van'
SUV = 'suv'
CAR_TYPES = (
(CAR, 'Car'),
(TRUCK, 'Truck'),
(VAN, 'Van'),
(SUV, 'SUV'),
)
submission_type = models.CharField(_('Submission Type'), max_length=20, choices=MEDIA_TYPES, default=CAR)
title = models.CharField(_('Title'), max_length=100, blank=False)
description = models.TextField(_('Description'))
user = models.ForeignKey(User, related_name='user_submission')
thumbnail = models.ImageField()
date_submitted = models.DateTimeField(default=timezone.now)
views.py
class SubmissionCategoryList(ListView):
model = Submission
template_name = 'submission/submit_cat.html'
def get_queryset(self):
queryset = super(SubmissionCategoryList, self).get_queryset()
return queryset.filter(submission_type=self.kwargs['slug']).order_by('-date_submitted')
def get_context_data(self, **kwargs):
context = super(SubmissionCategoryList, self).get_context_data(**kwargs)
return context
urls.py
url(r'^(?P<slug>[\w-]+)/$', SubmissionCategoryList.as_view(), name='submit_category'),
The code works fine. When I go to localhost:8000/car/ It shows the list view for only the CARS submission_type, etc. But, when I type in a url that isn't a part of the choices in CAR_TYPES, for example, localhost:8000/boat/, django still shows the template for this view. My question is: How do I limit the number of choices the slug should accept? And, if it is not a part of the CAR_TYPES choices, how do I get it to ignore this view?
Have you tried validating your "slug" in views?
views.py
def get_queryset(self):
if self.kwargs['slug'] is not None and self.kways['slug'].lower() in project.settings.CAR_TYPE:
# return your queryset.filter ...
else:
# return other template or redirect to other views.
project.settings.py
CAR_TYPE = ['car', 'truck', 'van', 'suv']
or, simply just edit url regex:
(?P<slug>car|truck|van|suv)
not the best approach but definitely solve your problem.
Ok so thanks to Anzel I figured out a great solution to this problem. I'm putting this here for anyone who needs this.
I created a reference.py file that looks like this. You can call it whatever you like, I just call it reference because it's what it does.
from enum import Enum
class CarTypes(Enum):
CAR = 'car'
TRUCK = 'truck'
VAN = 'van'
SUV = 'suv'
I changed my models.py to import reference.py
I changed choices in submission_type to choices=tuple([(auto.name, auto.value) for auto in CarTypes])
from .reference import CarTypes
class Submission(models.Model):
submission_type = models.CharField(_('Submission Type'), max_length=20, choices=tuple([(auto.name, auto.value) for auto in CarTypes]), default=CarTypes.CAR)
title = models.CharField(_('Title'), max_length=100, blank=False)
description = models.TextField(_('Description'))
user = models.ForeignKey(User, related_name='user_submissions')
thumbnail = models.ImageField()
date_submitted = models.DateTimeField(default=timezone.now)
And here is the final working views.py for categories
from .reference import CarTypes
class SubmissionCategoryList(ListView):
model = Submission
template_name = 'submission/submit_cat.html'
CAR_TYPES = [auto.value for auto in CarTypes]
def get_queryset(self):
if self.kwargs['slug'] in self.CAR_TYPES:
queryset = super(SubmissionCategoryList, self).get_queryset()
return queryset.filter(submission_type=self.kwargs['slug']).order_by('-date_submitted')
else:
return Http404
def get_context_data(self, **kwargs):
context = super(SubmissionCategoryList, self).get_context_data(**kwargs)
context['submission_type'] = self.kwargs['slug']
return context
You can put this same queryset in a DetailView and it should work when a user searches site.com/car/2 or something like that

Set a form field to the current logged user id

I have a class Task with the following implementation:
class Task(models.Model):
author = models.ForeignKey(Author, unique=False)
name = models.CharField(max_length=255)
completed = models.BooleanField(default=False)
deadline = models.DateTimeField(null=True, blank=True)
pub_date = models.DateTimeField(auto_now_add=True, editable=False)
edit_date = models.DateTimeField(auto_now_add=True, auto_now=True, editable=False)
tag = models.ManyToManyField(Tag, related_name='tags', null=True, blank=True, default=None)
# group = models.ForeignKey(Group, blank=True, default=None)
def __str__(self):
return u'%s' % (self.name)
def toggle_complete(self):
self.completed = not self.completed
def is_past_deadline(self):
return timezone.now() > self.deadline
And I am trying to do a simple form that creates a new Task with a Title. But, as you can see, the author attribute can not be null (and don't want to, of course).
Author is implemented as follows:
class Author(models.Model):
user = models.OneToOneField(User, primary_key=True)
name = models.CharField(max_length=30)
def __str__(self):
return u'%s' % (self.user)
I tried and tried to hide the author field and, overriding methods like get_form_kwargs, form_valid, get_form to set it to the current logged user, but I always fail. Simply, the id is neither sent as post data (as seein in the debug trace), nor fetched from the view itself.
My best result has been showing the author field, creating the user correctly, but getting a "success_url" not found, even with the model having a get_absolute_url method declared.
The view I am working with is implemented like:
class HomeView(CreateView, MultipleObjectMixin):
# common
model = models.Task
template_name = 'home.html'
#form
form_class = TaskForm
# list
object_list = model.objects.all()
context_object_name = 'tasks'
paginate_by = 40
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('taskr:index'))
return super(HomeView, self).dispatch(request, *args, **kwargs)
def get_form_kwargs(self):
kwargs = super(HomeView, self).get_form_kwargs()
kwargs['initial']['author_id'] = self.request.user.id
return kwargs
def form_valid(self, form):
task = form.save(commit=False)
task.user = models.Author.objects.get(user=self.request.user) # use your own profile here
task.save()
return HttpResponseRedirect(self.get_success_url())
For the record, the MultipleObjectMixing part of the view works flawlessly.
I am desperate, is there any good resource for Django forms, one like http://ccbv.co.uk/? Thanks.
After a good night sleep, while cleaning up, I tried fixing the form_valid in the CreateView descendant and I got it right.
The trick is in
task.user = models.Author.objects.get(user=self.request.user)
and it failed to me because of desperate copy-pasting. The problem was that my Task model has no user attribute, but an author. So
task.author = models.Author.objects.get(user=self.request.user)
fixes it all.
Sorry for the stupid question.

How to surface two Django models on one page?

I would like to create one page that surfaces two separate Django models:
class Client(models.Model):
name = models.CharField(max_length=100)
slug = AutoSlugField(populate_from='name', blank=True, unique=True)
order = models.IntegerField(editable=False, default=0)
class Meta:
ordering = ('order',)
def __unicode__(self):
return self.name
class Press(models.Model):
title = models.CharField(max_length=50)
article = models.ImageField(upload_to = 'images')
def image_thumb(self):
if self.article:
return u'<img src="%s" height="125"/>' %self.article.url
else:
return "no image"
image_thumb.short_description = "article"
image_thumb.allow_tags = True
class Meta:
verbose_name_plural = "press"
I am unsure how to write my queryset in Views.py. I've tried something like this...
class ClientView(generic.ListView):
template_name = 'clients.html'
context_object_name = 'client'
def queryset(request):
client_page = {'press': Press.objects.all(), 'client': Clients.objects.all()}
return client_page
and then this in my urls.py...
url(r'^clients/', views.ClientView.as_view(), name = 'client_model'),
I read in a stack answer that I can do this by using "get_extra_context" but can someone show me how that's used?
class ClientView(generic.ListView):
# ...
def get_context_data(self, **kwargs):
context = super(ClientView, self).get_context_data(**kwargs)
context['press'] = Press.objects.all()
return context

Getting context of parent in Foreign Key - Django

This is supposed to be really simple and it is in the documents. I am trying to get the context of a ListView. For example:
"We can also add the publisher into the context at the same time, so we can use it in the template http://docs.djangoproject.com/en/1.3/topics/class-based-views/#dynamic-filtering/:
class IssuesByTitleView(ListView):
context_object_name = "issue_list"
def get_queryset(self):
self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
return Issue.objects.filter(title=self.title).order_by('-number')
My models.py look something like this:
class Title(models.Model):
CATEGORY_CHOICES = (
('Ongoing', 'Ongoing'),
('Ongoing - Canceled', 'Ongoing - Canceled'),
('Limited Series', 'Limited Series'),
('One-shot', 'One-shot'),
('Other', 'Other'),
)
title = models.CharField(max_length=64)
vol = models.IntegerField(blank=True, null=True, max_length=3)
year = models.CharField(blank=True, null=True, max_length=20, help_text="Ex) 1980 - present, 1980 - 1989.")
category = models.CharField(max_length=30, choices=CATEGORY_CHOICES)
is_current = models.BooleanField(help_text="Check if the title is being published where Emma makes regular appearances.")
slug = models.SlugField()
class Meta:
ordering = ['title']
def get_absolute_url(self):
return "/titles/%s" % self.slug
def __unicode__(self):
return self.title
class Issue(models.Model):
CATEGORY_CHOICES = (
('Major', 'Major'),
('Minor', 'Minor'),
('Cameo', 'Cameo'),
('Other', 'Other'),
)
title = models.ForeignKey(Title)
number = models.IntegerField(help_text="Do not include the '#'.")
.........
Views.py:
class IssuesByTitleView(ListView):
context_object_name = "issue_list"
def get_queryset(self):
self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
return Issue.objects.filter(title=self.title).order_by('-number')
def get_context_data(self):
context = super(IssuesByTitleView, self).get_context_data()
context['title'] = self.title
return context
In my list of issues within the Title, I need to return the Title and all the other properties of the Title. That up there does not work and it returns an error:
get_context_data() got an unexpected keyword argument 'object_list'
You should use **kwargs in get_context_data
def get_context_data(self, **kwargs):
context = super(IssuesByTitleView, self).get_context_data(**kwargs)
context['title'] = self.title
return context