I'm a noob, so please excuse me if this is a silly request. I am trying to create custom RSS feeds for each category of a website, but somehow I can't manage to pass the parameter (a category slug) in order to properly build the requested feed. The RSS should be located at an address like this: http://www.website.com/category-name/feed
Here's what I have:
In urls.py:
from project.feeds import FeedForCategory
urlpatterns = patterns('category.views',
#...
url(r'^(?P<category_slug>[a-zA-Z0-9\-]+)/feed/?$', FeedForCategory),
)
In feeds.py:
from django.contrib.syndication.feeds import Feed
class FeedForCategory(Feed):
def get_object(self, request, category_slug):
return get_object_or_404(Category, slug_name=category_slug)
def title(self, obj):
return "website.com - latest stuff"
def link(self, obj):
return "/articles/"
def description(self, obj):
return "The latest stuff from Website.com"
def get_absolute_url(self, obj):
return settings.SITE_ADDRESS + "/articles/%s/" % obj.slug_name
def items(self, obj):
return Article.objects.filter(category=category_slug)[:10]
The error that I get is: "_ init _() got an unexpected keyword argument 'category_slug'", but the traceback isn't helpful, it only shows some base python stuff.
Thank you.
From the doc: https://docs.djangoproject.com/en/dev/ref/contrib/syndication/
You need to pass an instance of the feed object to your url patterns. So do this in urls.py:
from project.feeds import FeedForCategory
urlpatterns = patterns('category.views',
#...
url(r'^(?P<category_slug>[a-zA-Z0-9\-]+)/feed/?$', FeedForCategory()),
)
Related
I want all djaango urls use slug field without any parameter before or after, by default
just one url can use this metod
Views.py
class ArticleDetail(DetailView):
def get_object(self):
slug = self.kwargs.get('slug')
article = get_object_or_404(Article.objects.published(), slug=slug)
ip_address = self.request.user.ip_address
if ip_address not in article.hits.all():
article.hits.add(ip_address)
return article
class CategoryList(ListView):
paginate_by = 5
template_name = 'blog/category_list.html'
def get_queryset(self):
global category
slug = self.kwargs.get('slug')
category = get_object_or_404(Category.objects.active(), slug=slug)
return category.articles.published()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['category'] = category
return context
urls.py
urlpatterns = [
path('<slug:slug>', ArticleDetail.as_view(), name="detail"),
path('<slug:slug>', CategoryList.as_view(), name="category"),
]
This is my django blog codes,
I don't want write article or category & ... in urls, just slug
mysite .com/article-slug
...
mysite .com/category-slug
It will always trigger the Article view, regardless if there is an Article for that slug. You thus should make the URL patterns non-overlapping such that the other views can be triggered, for example with:
path('article/<slug:slug>/', Article.as_View(), name="articledetail"),
path('category/<slug:slug>/', Category.as_View(), name="category"),
path('product/<slug:slug>/', Product.as_View(), name="productdetail"),
If you want a path that accepts a single slug, you should define a view that looks if there is an Article with that slug, if that is not the case a Category and if that is not the case a Product you thus implement that logic in the view, not in the URL patterns.
#WillemVanOlsem is right, you will have to write a view like this:
from django.http import HttpResponseNotFound
def slug_router(request, slug):
if Category.objects.filter(slug=slug).exists():
return CategoryList.as_view()(request, slug=slug)
elif Article.objects.filter(slug=slug).exists():
return ArticleDetail.as_view()(request, slug=slug)
else:
return HttpResponseNotFound('404 Page not found')
And then
urlpatterns = [
path('<slug:slug>', slug_router, name="slug"),
]
... if I'm not mistaken. This should be the jist of it.
I didn't test this code, just typed it in here, so let me know if it doesn't work, I'll help to fix it.
Note that you'll have a preference if there are Articles with the same slug as some Categories.
I am new in Django and I am bit confused in Django apiview for custom method.
In ApiView, How can I create a custom method and How to call from axios.
For example
Here is my View
class TimeSheetAPIView(APIView):
#action(methods=['get'], detail=False)
def getbytsdate(self, request):
return Response({"timesheet":"hello from getbydate"})
def get(self,request,pk=None):
if pk:
timesheet=get_object_or_404(TimeSheet.objects.all(),pk=pk)
serializer = TimeSheetSerializer(timesheet)
return Response({serializer.data})
timesheet=TimeSheet.objects.all()
serializer = TimeSheetSerializer(timesheet,many=True)
return Response({"timesheet":serializer.data})
Here is my URL=>
url(r'^timesheets_ts/(?P<pk>\d+)/$', TimeSheetAPIView.as_view()),
url(r'^timesheets_ts/', TimeSheetAPIView.as_view()),
Normally my url would be like=>
api/timesheet_ts/
this one will get all of my records.
So my question is how can I setup URL for getbytsdate or getbyname or other some kind of custom get method? and how can I call?
I tried like this way=>
url(r'^timesheets_ts/getbytsdate/(?P<tsdate>[-\w]+)/$', TimeSheetAPIView.as_view()),
and I called like that
api/timesheets_ts/getbytsdate/?tsdate='test'
Its not work.
So please can u explain for the custom method in apiview and url setting?
In addition to your implementation, you just need to show your custom get request to your urls.py. Edit your urls.py as follows:
# urls.py
timesheet_getbytsdate_detail = TimeSheetAPIView.as_view({'get': 'getbytsdate'})
timesheet_detail = TimeSheetAPIView.as_view({'get': 'retrieve'})
urlpatterns = [
url(r'^timesheets_ts/getbytsdate/(?P<tsdate>[-\w]+)/$', getbytsdate_detail),
url(r'^timesheets_ts/(?P<pk>[0-9]+)/', timesheet_detail),
]
EDIT: You need to use the combination viewsets.GenericViewSet and mixins.RetrieveModelMixin instead of APIVewto get use of that:
class TimeSheetAPIView(viewsets.GenericViewSet, mixins.RetrieveModelMixin):
#action(methods=['get'], detail=False)
def getbytsdate(self, request):
return Response({"timesheet":"hello from getbydate"})
def retrieve(self, request, *args, **kwargs):
timesheet=self.get_object()
serializer = TimeSheetSerializer(timesheet)
return Response({serializer.data})
timesheet=TimeSheet.objects.all()
serializer = TimeSheetSerializer(timesheet,many=True)
return Response({"timesheet":serializer.data})
My search results page needs to display information about the Plugins where the query was found, too. I found this question with a similar problem, but I don't only need the contents, I need to know stuff about the plugin - i.e. what's it called, where it is on the page and stuff. Basically I would like a reference to the plugin where the query was located, but I can only find information about the page and title. I haven't been able to find it anywhere on the SearchQuerySet object and in the vicinity - also coming up empty in the documentation for Haystack. Is it possible and how?
Stack I'm using: Elasticsearch 2.4, django-haystack 2.8, aldryn-search 1.0 (for CMS indexing).
I ended up writing a new index for CMSPlugins. Not sure how much use my code is, but maybe it'll help someone out.
from django.conf import settings
from aldryn_search.helpers import get_plugin_index_data
from aldryn_search.utils import clean_join, get_index_base
from cms.models import CMSPlugin
class CMSPluginIndex(get_index_base()):
haystack_use_for_indexing = True
index_title = True
object_actions = ('publish', 'unpublish')
def get_model(self):
return CMSPlugin
def get_index_queryset(self, language):
return CMSPlugin.objects.select_related(
'placeholder'
).prefetch_related(
'placeholder__page_set'
).filter(
placeholder__page__publisher_is_draft=False,
language=language
).exclude(
plugin_type__in=settings.HAYSTACK_EXCLUDED_PLUGINS
).distinct()
def get_search_data(self, obj, language, request):
current_page = obj.placeholder.page
text_bits = []
plugin_text_content = self.get_plugin_search_text(obj, request)
text_bits.append(plugin_text_content)
page_meta_description = current_page.get_meta_description(fallback=False, language=language)
if page_meta_description:
text_bits.append(page_meta_description)
page_meta_keywords = getattr(current_page, 'get_meta_keywords', None)
if callable(page_meta_keywords):
text_bits.append(page_meta_keywords())
return clean_join(' ', text_bits)
def get_plugin_search_text(self, base_plugin, request):
plugin_content_bits = get_plugin_index_data(base_plugin, request)
return clean_join(' ', plugin_content_bits)
def prepare_pub_date(self, obj):
return obj.placeholder.page.publication_date
def prepare_login_required(self, obj):
return obj.placeholder.page.login_required
def get_url(self, obj):
parent_obj = self.ancestors_queryset(obj).first()
if not parent_obj:
return obj.placeholder.page.get_absolute_url()
return # however you get the URL in your project
def get_page_title_obj(self, obj):
return obj.placeholder.page.title_set.get(
publisher_is_draft=False,
language=obj.language
)
def ancestors_queryset(self, obj):
return obj.get_ancestors().filter(
plugin_type=# Some plugins that I wanted to find
).order_by(
'-depth'
)
def get_title(self, obj):
parent_obj = self.ancestors_queryset(obj).first()
if not parent_obj:
return self.get_page_title_obj(obj).title
return # get title from parent obj if you want to
def prepare_site_id(self, obj):
return obj.placeholder.page.node.site_id
def get_description(self, obj):
return self.get_page_title_obj(obj).meta_description or None
If you are using aldryn-search, you only need to define in PLACEHOLDERS_SEARCH_LIST all the placeholders you want to check, therefore all plugins inside will be checked:
PLACEHOLDERS_SEARCH_LIST = {
'*': {
'include': ['content'],
'exclude': [''],
},
}
Following is my program
import os
import jinja2
import re
from string import letters
import webapp2
from google.appengine.ext import db
template_dir= os.path.join(os.path.dirname(__file__),'templates')
jinja_env= jinja2.Environment(loader= jinja2.FileSystemLoader(template_dir),autoescape= True)
class Handler(webapp2.RequestHandler):
def write(self,*a,**kw):
self.response.write(*a,**kw)
def render_str(self,template, **params):
t= jinja_env.get_template(template)
return t.render(params)
def render(self,template, **kw):
self.write(self.render_str(template,**kw))
def blog_key(name = "default"):
return db.Key.from_path('blogs',name)
class Post(db.Model):
title= db.StringProperty(required= True)
content= db.TextProperty(required= True)
created= db.DateTimeProperty(auto_now_add= True)
edited= db.DateTimeProperty(auto_now= True)
def render(self):
self._render_text= self.content.replace('\n', '<br>')
return render_str("blogs.html", p= self)
class BlogFront(Handler):
def get(self):
posts= db.GqlQuery("select * from posts order by desc limit 10")
self.render("front.html", posts=posts )
#for a link to the new posts created
class PostPage(Handler):
def get(self, post_id):
key= db.Key.from_path("Post", int(post_id), parent= blog_key())
post=db.get(key)
if not post:
self.error(404)
return
self.render("permalink.html", post=post)
#for new blog entries
class NewPost(Handler):
def get(self):
self.render('newpost.html')
def post(self):
title= self.request.get("title")
content= self.request.get("content")
if title and content:
p= Post(parent= blog_key(), title=title, content=content)
p.put()
self.redirect('/blogs/%s' % str(p.key().id()))
else:
error= "Please write both title and content!!"
self.render("newpost.html",title=title, content=content, error=error)
app = webapp2.WSGIApplication([
('/blog/newpost', NewPost),
('/blogs/?',BlogFront),
('/blogs/([0-9]+)',PostPage), #anything in the bracket will be passed as the parameter
], debug=True)
But when I'm trying to implement this program, I'm getting the following error:
File "C:\Users\tan31102\AppData\Local\Google\Cloud
SDK\google-cloud-sdk\platfo
rm\google_appengine\google\appengine\ext\db__init__.py", line 299, in
class_for
_kind
raise KindError('No implementation for kind \'%s\'' % kind) KindError: No implementation for kind 'posts'
Can someone please help me with this.
Your gql uses post in plural form - "posts"
select * from posts order by desc limit 1
while the db.Model class you have declared uses posts in singular form (Post)
class Post(db.Model):
You need to stick with either form. Also you should consider using ndb.Model instead of db.Model as stated in the docs
You have to import the model definition in the Py file that uses it. This causes the KindError.
from posts import posts
or something similar.
It sounds like you are trying to load a posts entity from a session without importing the posts model first.
To ensure that posts is available when the session middleware runs, you must import the posts model in your script.
In django admin I wanted to set up a custom filter by tags (tags are introduced with django-tagging)
I've made the ModelAdmin for this and it used to work fine, by appending custom urlconf and modifying the changelist view. It should work with URLs like: http://127.0.0.1:8000/admin/reviews/review/only-tagged-vista/
But now I get 'invalid literal for int() with base 10: 'only-tagged-vista', error which means it keeps matching the review edit page instead of the custom filter page, and I cannot figure out why since it used to work and I can't find what change might have affected this.
Any help appreciated.
Relevant code:
class ReviewAdmin(VersionAdmin):
def changelist_view(self, request, extra_context=None, **kwargs):
from django.contrib.admin.views.main import ChangeList
cl = ChangeList(request, self.model, list(self.list_display),
self.list_display_links, self.list_filter,
self.date_hierarchy, self.search_fields,
self.list_select_related,
self.list_per_page,
self.list_editable,
self)
cl.formset = None
if extra_context is None:
extra_context = {}
if kwargs.get('only_tagged'):
tag = kwargs.get('tag')
cl.result_list = cl.result_list.filter(tags__icontains=tag)
extra_context['extra_filter'] = "Only tagged %s" % tag
extra_context['cl'] = cl
return super(ReviewAdmin, self).changelist_view(request, extra_context=extra_context)
def get_urls(self):
from django.conf.urls.defaults import patterns, url
urls = super(ReviewAdmin, self).get_urls()
def wrap(view):
def wrapper(*args, **kwargs):
return self.admin_site.admin_view(view)(*args, **kwargs)
return update_wrapper(wrapper, view)
info = self.model._meta.app_label, self.model._meta.module_name
my_urls = patterns('',
# make edit work from tagged filter list view
# redirect to normal edit view
url(r'^only-tagged-\w+/(?P<id>.+)/$',
redirect_to,
{'url': "/admin/"+self.model._meta.app_label+"/"+self.model._meta.module_name+"/%(id)s"}
),
# tagged filter list view
url(r'^only-tagged-(P<tag>\w+)/$',
self.admin_site.admin_view(self.changelist_view),
{'only_tagged':True}, name="changelist_view"),
)
return my_urls + urls
Edit: Original issue fixed.
I now receive 'Cannot filter a query once a slice has been taken.' for line:
cl.result_list = cl.result_list.filter(tags__icontains=tag)
I'm not sure where this result list is sliced, before tag filter is applied.
Edit2:
It's because of the self.list_per_page in ChangeList declaration. However didn't find a proper solution yet. Temp fix:
if kwargs.get('only_tagged'):
list_per_page = 1000000
else:
list_per_page = self.list_per_page
cl = ChangeList(request, self.model, list(self.list_display),
self.list_display_links, self.list_filter,
self.date_hierarchy, self.search_fields,
self.list_select_related,
list_per_page,
self.list_editable,
self)
You're missing a question mark in before the P in '^only-tagged-(P<tag>\w+)/$', so the expression won't match.
In code sample above, get_urls() is aligned so that it is not part of ReviewAdmin class but rather a separate function. I guess that can cause your problem if you have it the same way in real source.
The error apears on multi-word tags because you match just one word tags.
this works: r'^only-tagged-(?P[^/]+)/$'