I have a Django/Wagtail/Puput site with this structure:
RootPage
|
|- BlogPage (Puput)
|- InformationPage
I'm trying to display summary info from the Puput blog on the InformationPage. This works with this code, as long as I only have one BlogPage:
class InformationPage(Page):
body = RichTextField(verbose_name=_("body"))
. . .
def get_context(self, request, *args, **kwargs):
context = super(InformationPage, self).get_context(
request, *args, **kwargs)
context['blog_page'] = BlogPage.objects.first()
context['information_page'] = self
return context
But I'm trying to make it work with more than one blog page. It seems like this should work:
class InformationPage(Page):
body = RichTextField(verbose_name=_("body"))
blog_page = models.ForeignKey('wagtailcore.Page', on_delete=models.PROTECT, related_name="information_blog")
content_panels = [
MultiFieldPanel(
[
FieldPanel("title", classname="title"),
FieldPanel("body", classname="full"),
PageChooserPanel('blog_page'),
],
heading=_("Content"),
)]
def get_context(self, request, *args, **kwargs):
context = super(InformationPage, self).get_context(
request, *args, **kwargs)
context['blog_page'] = self.blog_page
context['information_page'] = self
return context
But it doesn't. This was suggested by #gasman here. In other words, if I refer to the blog page properties using context['blog_page'] = BlogPage.objects.first(), everything works fine, but switching it out to use context['blog_page'] = self.blog_page (and selecting the correct blog page in the admin) does not work.
Without switching it out, I think I can only ever have a single instance of BlogPage, because all InformationPages will have to pull from the first instance.
Any thoughts?
You haven't given a description of the problem beyond "it doesn't work", so I'm only guessing here, but you're presumably trying to output fields of the blog page that are part of the BlogPage model. This doesn't work because blog_page is defined as a foreign key to wagtailcore.Page, and so accessing self.blog_page only gives you a basic Page object consisting of the core fields such as title. You can retrieve the full BlogPage object by accessing self.blog_page.specific:
context['blog_page'] = self.blog_page.specific
However, a smarter approach is to change your foreign key to point to BlogPage, since presumably choosing any other page type is not valid here:
blog_page = models.ForeignKey('my_blog_app.BlogPage', on_delete=models.PROTECT, related_name="information_blog")
With this change, self.blog_page will return a BlogPage instance directly, and there's no need for .specific.
Related
Currently, when searching for a tag like Instagram, an image list page including the tag has been created. I wanted to list the related tag list in one line here, and I found out that the 'taggit' package already has a good function called 'similar_objects'. My 'view.py' is designed to expose only certain tags as follows. If you add "similar_objects,"
"'ImageTagListView' object has no attribute 'object'" occurs.
I think it's a "queryset" problem. Please tell me the solution. Thank you in advance.
class ImageTagListView(ImageListView):
template_name = 'imageapp/taglist.html'
model = Image
def get_queryset(self):
return Image.objects.filter(tags__name=self.kwargs.get('tag'))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['tagname'] = self.kwargs['tag']
context["related_items"] = self.object.tags.similar_objects()[:4]
return context
Friends,
I have a Django app and I want to add some basic tracking for all my views. (Much like a counter or something similar)
What I have so far is that I can track specific objects with mixins. So every time someone is clicking on an instance of my model (the DetailView) an entry is added to my database. I did this via the django content types.
Now, to do this I need a get method to actually get a specific object back.
But in my ListView I don't have that object.
How could I implement something similar for either urls or just my ListView? Is that even possible? I'd like to record a single entry stating that the list of my model has been accessed.
Here is what I have so far:
my views
class ListJobView(ObjectViewMixin, ListView):
model = Job
context_object_name = 'jobs'
template_name = 'list_jobs.html'
ordering = '-pub_date'
# paginate_by = 1
class DetailJobView(ObjectViewMixin, DetailView):
model = Job
template_name = 'detail_job.html'
queryset = Job.objects.all()
def get_object(self):
id = self.kwargs.get("id")
return get_object_or_404(Job, id=id)
my mixin
from .signals import object_viewed_signal
class ObjectViewMixin:
def dispatch(self, request, *args, **kwargs):
try:
instance = self.get_object()
except self.model.DoesNotExist:
instance = None
if instance is not None:
object_viewed_signal.send(instance.__class__, instance=instance, request=request)
return super(ObjectViewMixin, self).dispatch(request, *args, **kwargs)
my signal
from django.dispatch import Signal
object_viewed_signal = Signal(providing_args=['instance', 'request'])
here is the signal handler:
def object_viewed_receiver(sender, instance, request, *args, **kwargs):
new_viewed_object = ObjectViewed.objects.create(
user = request.user,
content_type = ContentType.objects.get_for_model(sender),
object_id = instance.id,
)
object_viewed_signal.connect(object_viewed_receiver)
If I should provide more code please let me know.
Any help is highly appreciated...
So this answer is without any guarantees to work. I implemented this solution at the time I was asking the question but I am not using it anymore since I removed this functionality from my django app. A reason for this was the heavy load. Because in this solution I create an object for every time someone visits the website and save it to the DB. This works fine but gets really heavy for the DB after a while.
First I created and new app and called it analytics. In the app I created model like this:
class UrlTime(models.Model):
associated_url = models.CharField(blank=True, null= True, max_length=250)
timestamp = models.DateTimeField(auto_now=True, blank=True)
def __str__(self):
return self.associated_url
I created a new module called middleware.py. A middleware basically executes code either before a view is called or after a view is called (dependent where you put the code). If you count URLs I think it's good to have it before a view is called:
middleware.py
from .models import UrlTime
class GetUrlMiddleware():
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# before view
# creates a counter object for URL if url doesn't exist. Else counts up.
if request.path.startswith('/admin/'): ##exclude what you don't want to count
pass
else:
# urltrack, _ = UrlTrack.objects.get_or_create(url=request.path)
# urltrack.counter = F('counter') + 1
# urltrack.save()
urltime = UrlTime.objects.create(associated_url=request.path)
urltime.save()
response = self.get_response(request)
# after view
return response
With this you should be able to count your URls every time someone visits the page. An object will be created in the DB. Then you only have to display it how you want it on a template. I did something like this:
class AnalyticsIndexView(StaffRequiredMixin, ListView):
template_name = 'analytics_list.html'
model = UrlTime
def get_context_data(self, **kwargs):
context = super(AnalyticsIndexView, self).get_context_data(**kwargs)
context['object_viewed_list'] = ObjectViewed.objects.all()
context['url_views_list'] = UrlTime.objects.all()
context['total_views'] = UrlTime.objects.all().count
counting = UrlTime.objects.all().values('associated_url').annotate(url_count=Count('associated_url'))
context['home_view'] = counting
context['start'] = UrlTime.objects.first()
return context
Then you just need to implement the template.... If you need that too let me know and I post it here too.
I've seen a similar question, alas it has not been answered.
I have an app that features Entries (like blog entries) which include a part called SubEntry. I want the users to be able to report SubEntries (i.e. press the button 'report', fill some fields and the application sends an email to admins, saving the report in db is nice to have):
The flow should be like that: at the view EntryDetails (url: /entry/entry-title/) the user may click on the SubEntry part. The modal opens and the subentry is visualized in the modal as enlarged, with a button/link underneath 'Report the SubEntry'. Then it's possible to click on the 'Report the SubEntry' button and two fields appear - reason of reporting and contact detail of the reporter (here I am just toggling the visibility of the fields). I manage to display the form (with get overriden - overriding get_form_kwargs causes the error No Entry with that title) but either the Entry or its attributes are not displayed...
My questions are:
1) is creating a model for Reporting (ReportSubEntry) a decent approach?
2) I can't seem to pass the needed variable (an Entry object that is to be a ForeignKey for a SubEntry object that is being created) from CreateReport view to the report_subentry.html.
any thoughts, advice? Python 3.5, Django 1.10
models.py:
class ReportSubentry(models.Model):
Entry = models.ForeignKey('Entry')
details = models.CharField(max_length=100)
contact = models.EmailField()
forms.py:
class ReportEntryForm(forms.ModelForm):
class Meta:
model = ReportSubEntry
fields = ['details', 'contact', 'project']
views.py:
class CreateReport(CreateView):
model = ReportSubEntry
form_class = ReportSubEntryForm
template_name = 'understand/report_subentry.html'
# tried two methods to pass the variables:
def get(self, request, *args, **kwargs):
self.object = None
title = kwargs.get('title')
kwargs['entry'] = get_object_or_404(Entry, title=title)
return super(CreateReport, self).get(request, **kwargs)
def get_form_kwargs(self, **kwargs):
title = kwargs.get('title')
kwargs['entry'] = get_object_or_404(Entry, title=title)
return kwargs
The current model that you are using ReportSubEntry is perfect and there is no need to change it.
In your forms.py ReportEntryForm you have to use relatedfields to be able to correctly serialize the data. There is no need to override anything. When user clicks on report the sub entry you have to pass the pk of Entry model as it is required to know which entry is reported. I am assuming that since you are successfully displaying the entries pk of those are present. When you receive the pk with other two fields you get the corresponding entry for pk and then pass the object to ReportSubentry.objects.create method.
The reportentry form should not contain foreign key. You have two choices for that. First is remove that field and pass the pk of entry from frontend using ajax calls or use javascript to add a disabled input field which contains pk of entry when user clicks on report subentry.
Ok, so I've solved this issue.
The only solution that worked for me was overriding the get method of the ReportSubentry without calling the get method of the superclass:
def get(self, request, *args, **kwargs):
self.object = None
title = kwargs.get('title')
entry = get_object_or_404(Entry, title=title)
context_data = self.get_context_data()
context_data.update(entry=entry)
return self.render_to_response(context_data)
Please feel free to discuss it.
I'm trying to implement partial_update with Django Rest Framework but I need some clarification because I'm stuck.
Why do we need to specify partial=True?
In my understanding, we could easily update Demo object inside of partial_update method. What is the purpose of this?
What is inside of serialized variable?
What is inside of serialized variable in partial_update method? Is that a Demo object? What function is called behind the scenes?
How would one finish the implementation here?
Viewset
class DemoViewSet(viewsets.ModelViewSet):
serializer_class = DemoSerializer
def partial_update(self, request, pk=None):
serialized = DemoSerializer(request.user, data=request.data, partial=True)
return Response(status=status.HTTP_202_ACCEPTED)
Serializer
class DemoSerializer(serializers.ModelSerializer):
class Meta:
model = Demo
fields = '__all__'
def update(self, instance, validated_data):
print 'this - here'
demo = Demo.objects.get(pk=instance.id)
Demo.objects.filter(pk=instance.id)\
.update(**validated_data)
return demo
I when digging into the source code of rest_framework and got the following findings:
For question 1. Why do we need to specify partial=True?
This question is related to HTTP verbs.
PUT: The PUT method replaces all current representations of the target resource with the request payload.
PATCH: The PATCH method is used to apply partial modifications to a resource.
Generally speaking, partial is used to check whether the fields in the model is needed to do field validation when client submitting data to the view.
For example, we have a Book model like this, pls note both of the name and author_name fields are mandatory (not null & not blank).
class Book(models.Model):
name = models.CharField('name of the book', max_length=100)
author_name = models.CharField('the name of the author', max_length=50)
# Create a new instance for testing
Book.objects.create(name='Python in a nut shell', author_name='Alex Martelli')
For some scenarios, we may only need to update part of the fields in the model, e.g., we only need to update name field in the Book. So for this case, client will only submit the name field with new value to the view. The data submit from the client may look like this:
{"pk": 1, name: "PYTHON IN A NUT SHELL"}
But you may have notice that our model definition does not allow author_name to be blank. So we have to use partial_update instead of update. So the rest framework will not perform field validation check for the fields which is missing in the request data.
For testing purpose, you can create two views for both update and partial_update, and you will get more understanding what I just said.
Example:
views.py
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import UpdateModelMixin
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
class BookUpdateView(GenericAPIView, UpdateModelMixin):
'''
Book update API, need to submit both `name` and `author_name` fields
At the same time, or django will prevent to do update for field missing
'''
queryset = Book.objects.all()
serializer_class = BookSerializer
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
class BookPartialUpdateView(GenericAPIView, UpdateModelMixin):
'''
You just need to provide the field which is to be modified.
'''
queryset = Book.objects.all()
serializer_class = BookSerializer
def put(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
urls.py
urlpatterns = patterns('',
url(r'^book/update/(?P<pk>\d+)/$', BookUpdateView.as_view(), name='book_update'),
url(r'^book/update-partial/(?P<pk>\d+)/$', BookPartialUpdateView.as_view(), name='book_partial_update'),
)
Data to submit
{"pk": 1, name: "PYTHON IN A NUT SHELL"}
When you submit the above json to the /book/update/1/, you will got the following error with HTTP_STATUS_CODE=400:
{
"author_name": [
"This field is required."
]
}
But when you submit the above json to /book/update-partial/1/, you will got HTTP_STATUS_CODE=200 with following response,
{
"id": 1,
"name": "PYTHON IN A NUT SHELL",
"author_name": "Alex Martelli"
}
For question 2. What is inside of serialized variable?
serialized is a object wrapping the model instance as a serialisable object. and you can use this serialized to generate a plain JSON string with serialized.data .
For question 3. How would one finish the implementation here?
I think you can answer yourself when you have read the answer above, and you should have known when to use update and when to used partial_update.
If you still have any question, feel free to ask. I just read part of the source code of the rest framework, and may have not understand very deeply for some terms, and please point it out when it is wrong...
For partial update - PATCH http method
For full update - PUT http method
When doing an update with DRF, you are supposed to send request data that includes values for all (required) fields. This is at least the case when the request is via the PUT http method. From what I understand, you want to update one or at least not all model instance fields. In this case make a request with the PATCH http method. Django rest framework (DRF) will take care of it out of the box.
Example (with token auth):
curl -i -X PATCH -d '{"name":"my favorite banana"}' -H "Content-Type: application/json" -H 'Authorization: Token <some token>' http://localhost:8000/bananas/
So simple, just override init method of your serializer like that:
def __init__(self, *args, **kwargs):
kwargs['partial'] = True
super(DemoSerializer, self).__init__(*args, **kwargs)
Just a quick note as it seems that nobody has already pointed this out:
serialized = DemoSerializer(request.user, data=request.data, partial=True)
The first argument of DemoSerializer should be a Demo instance, not a user (at least if you use DRF 3.6.2 like me).
I don't know what you are trying to do, but this is a working example:
def partial_update(self, request, *args, **kwargs):
response_with_updated_instance = super(DemoViewSet, self).partial_update(request, *args, **kwargs)
Demo.objects.my_func(request.user, self.get_object())
return response_with_updated_instance
I do the partial update and then I do other things calling my_func and passing the current user and the demo instance already updated.
Hope this helps.
I had an issue where my multi-attribute/field validation in a rest_framework serializer was working with a POST /resources/ request but failing with a PATCH /resources/ request. It failed in the PATCH case because it was only looking for values in the supplied attrs dict and not falling back to values in self.instance. Adding a method get_attr_or_default to do that fallback seems to have worked:
class EmailSerializer(serializers.ModelSerializer):
def get_attr_or_default(self, attr, attrs, default=''):
"""Return the value of key ``attr`` in the dict ``attrs``; if that is
not present, return the value of the attribute ``attr`` in
``self.instance``; otherwise return ``default``.
"""
return attrs.get(attr, getattr(self.instance, attr, ''))
def validate(self, attrs):
"""Ensure that either a) there is a body or b) there is a valid template
reference and template context.
"""
existing_body = self.get_attr_or_default('body', attrs).strip()
if existing_body:
return attrs
template = self.get_attr_or_default('template', attrs)
templatecontext = self.get_attr_or_default('templatecontext', attrs)
if template and templatecontext:
try:
render_template(template.data, templatecontext)
return attrs
except TemplateRendererException as err:
raise serializers.ValidationError(str(err))
raise serializers.ValidationError(NO_BODY_OR_TEMPLATE_ERROR_MSG)
I don't know why, but for me, the only way to solve it was to override the validate method in the Serializer class.
Maybe it's related to the fact that I'm using MongoDB with Djongo
class DemoSerializer(serializers.ModelSerializer):
def validate(self, attrs):
self._kwargs["partial"] = True
return super().validate(attrs)
You forgot serializer.save()
You can finish it the following way . . .
class DemoViewSet(viewsets.ModelViewSet):
serializer_class = DemoSerializer
def partial_update(self, request, pk=None):
serializer = DemoSerializer(request.user, data=request.data, partial=True)
serializer.save()
serializer.is_valid(raise_exception=True)
return Response(serializer.data)
Also, you shouldn't need to override the update method in the serializer.
I'm fair new to Django and I am trying to log visitor for my blog. I'm using generic view for my blog and here is part of code:
#blog/urls.py
urlpatterns = patterns('',
#index
url(r'^(?P<page>\d+)?/?$', PostListView.as_view(
model=Post,
paginate_by=3,
)),
#individual post
url(r'^(?P<pub_date__year>\d{4})/(?P<pub_date__month>\d{1,2})/(?P<slug>[a-zA-Z0-9-]+)/?$',
DetailView.as_view(model=Post,)),
#cat
url(r'^category/(?P<slug>[a-zA-Z0-9]+)/?$', CategoryListView.as_view(
paginate_by=3,
model=Category,
)),
#tag
url(r'^tag/(?P<slug>[a-zA-Z0-9]+)/?$', TagListView.as_view(
paginate_by=3,
model=Tag,
)),
and I wrote a simple model for visitor log:
#tasks/models.py
class Visitor(models.Model):
visit_stamp = models.DateTimeField(auto_now_add=True)
referer = models.CharField(max_length=100, blank=True)
ip = models.IPAddressField(blank=True)
user_agent = models.CharField(max_length=100, blank=True)
page = models.CharField(max_length=100)
and its view:
#tasks/views.py
def log(request, page):
try:
hit = Visitor()
hit.page = page
hit.ip = request.META.get('REMOTE ADDR', '')
hit.last_visit = datetime.now()
hit.referer = request.META.get('HTTP REFERER', '')
hit.user_agent = request.META.get('HTTP_USER_AGENT', '')
hit.save()
except IntegrityError:
pass
def tracking(request, page):
log(request, page)
return render_to_response(page)
My question is how and where can I call this methods so that I can log a user is visiting a specific page. I'd appreciate any advices.
First off, I assume you don't have access to the apache (or whatever host is running your django app) logs and/or you want to eventually add other things and/or you want it available in the database, as otherwise, you can skip a lot of work and just grep the logs.
Anyways, I'd recommend rewriting track to work as a decorator (and adjust log as you need it... note that I believe you can get the URL from the request object versus passing it in as a page value in case you want to know which specific instance was visited). There are also ways you could probably do this with middleware, but this gives you a pretty good mix of simplicity and ability to control which views get logged.
To borrow an example from http://www.djangofoo.com/253/writing-django-decorators
def track(page):
def decorator(func):
def inner_decorator(request, *args, **kwargs):
log(request, page)
return func(request, *args, **kwargs)
return wraps(func)(inner_decorator)
return decorator
And then in your urls (or you can also do #track to decorate function based views)
url(r'^(?P<page>\d+)?/?$', track("index")(PostListView.as_view(
model=Post,
paginate_by=3,
))),
url(r'^someregexp$', track("pagename")(SomeListView.as_view(
model=Post,
paginate_by=3,
))),
Edit: meant to add. Do note that in general, GET requests are supposed to be idempotent; logging is a gray area but the main thing to keep in mind is that some requests may not get logged as you might expect if the page is cached (for Posts this shouldn't be an issue)