It's an ImportError (ImportError at /news Could not import 'news'. The path
must be fully qualified.) and trying to follow the traceback have narrowed
it down to either how I am importing the model in my views (if that's even
necessary) or to how I am calling the defined `news` view in the app's
urlconf.
views.py:
from .models import News, NewsCategory
def news(request, slug):
return HttpResponse('Test')
urls.py
from .models import News
import views
urlpatterns = [
'news.views',
url("^%s(?P<slug>.*)%s$" % _slashes, "news", name="news"),
]
And the root urls.py pattern, with `kdi` being the name of my app:
url(r'^news/',include('kdi.urls')),
Slowly trying to reverse engineer the blog's views and urls to get to a
pared down version, but there is a lot going on. Because my custom
Displayable model does not need to be Ownable, there is no need to be any
calling of the User model or referencing the request.user or author; there
is also no need for a Detail view or feeds, or much filtering at all. That
leaves only date and category in the pared views.py. That leaves something
like this:
def news(request, tag=None, year=None, month=None, category=None,
template="blog/blog_post_list.html", extra_context=None):
templates = []
blog_posts = BlogPost.objects.published(for_user=request.user)
if tag is not None:
tag = get_object_or_404(Keyword, slug=tag)
blog_posts = blog_posts.filter(keywords__keyword=tag)
if year is not None:
blog_posts = blog_posts.filter(publish_date__year=year)
if month is not None:
blog_posts = blog_posts.filter(publish_date__month=month)
try:
month = _(month_name[int(month)])
except IndexError:
raise Http404()
if category is not None:
category = get_object_or_404(BlogCategory, slug=category)
blog_posts = blog_posts.filter(categories=category)
templates.append(u"templates/blog_post_list_%s.html" %
str(category.slug))
prefetch = ("categories", "keywords__keyword")
blog_posts =
blog_posts.select_related("user").prefetch_related(*prefetch)
blog_posts = paginate(blog_posts, request.GET.get("page", 1),
settings.BLOG_POST_PER_PAGE,
settings.MAX_PAGING_LINKS)
context = {"blog_posts": blog_posts, "year": year, "month": month,
"tag": tag, "category": category, "author": author}
context.update(extra_context or {})
templates.append(template)
return TemplateResponse(request, templates, context)
And this is where I have some questions. Going line by line, `
BlogPost.objects.published(for_user=request.user)`, would removing the
`request.user` return all of the published objects, in this case
News.objects?
Filtering by tags, dates, and categories makes sense, until we reach
select_related `blog_posts =
blog_posts.select_related("user").prefetch_related(*prefetch)`. Because
this is focused on the user which is not necessary, is it possible to just
prefetch all posts with the custom model, or to select_related based on the
category?
Pagination is also not necessary for this use case, so is it possible to
remove entirely `blog_posts = paginate(blog_posts, request.GET.get("page",
1), settings.BLOG_POST_PER_PAGE, settings.MAX_PAGING_LINKS)`? The hacky
solution would be to keep this bit of code, and then set
`BLOG_POST_PER_PAGE` to a large number in settings.
And then in the `context` dict, is it safe to remove unneeded key/val
pairs? Specifically `author`? Because there is so much heavy lifting going
on with the blog that's not needed here, I'm afraid of breakage.
Now looking at simplifying urlpatterns so that it will successfully direct
to the News model's get_absolute_url + the object's slug (something like
http://x/news/slug):
'news.views',
url("^%s(?P<slug>.*)%s$" % _slashes, "news", name="news"),
Is this structured correctly? Should the slug come after the absolute url?
Something like `url("^news/(?P<slug>.*)%s$" % _slashes, "news", name="news")
`?
Sorry to ask so many questions, and thank you for your help! I've been a
bit intimidated by Django's urls.py and views.py for a while, and have
managed to do almost all of my logic through custom models and custom
templatetags. Even just learning the correct things to google helps a ton,
and I appreciate any help at all!
On Friday, July 7, 2017 at 8:09:00 AM UTC-7, Eduardo Rivas wrote:
>
> Could you share the error message you’re getting?
>
--
You received this message because you are subscribed to the Google Groups
"Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.