1

I've made a "paginator" app, that add such SEO optimisation for all my pages. So I need to pass all visible page url through paginator.view But, I want to keep my apps as structured as possible.

For an example here is a view for my gallery app:

gallery.views

from django.shortcuts import render
from gallery.models import GalleryItem

def home(request):
    img_to_display = GalleryItem.objects.filter(published=True
                    ).order_by('-date')
    return render(request, 'gallery/all.html', locals())
...

Now I'm doing like that form my view in paginator :

My current paginator.views

from django.shortcuts import render, get_object_or_404, redirect

from gallery.models import GalleryItem
from paginator.models import Page

import gallery


def custom_page(request, url):
    current_page = url

    # just for the gallery page :
    if url == 'gallery':
        img_to_display = GalleryItem.objects.filter(published=True
                            ).order_by('-date')
   
    # for all my page
    page_to_load = get_object_or_404(Page, name=url)
    template_to_load = "paginator/" + page_to_load.template_name
    
    return render(request, template_to_load, locals())

So I copy/paste my view and all dependencies, but that is really ugly, and not at all DRY, worth it's not maintainable. I try something like that but it doesn't work :

paginator.views : option1

from django.shortcuts import render

import gallery


def custom_page(request, url):
    if url == 'gallery':
        gallery.views.home(request)
    if url == 'anotherpage':
        anotherapp.views.home(request)
...

Or something like that :

paginator.views : option 2

from django.shortcuts import render

def custom_page(request, url):
    if url == 'gallery':
        include("gallery.views.py")
    if url == 'anotherpage':
        include("anotherapp.views.py")
...

Note: I prefer the last style option because it minimize the import at the start of the paginator.views file.

Thanks a lot for helping ! :)

5
  • Do you want to trigger gallery.home view from paginator.custom_page? Commented May 2, 2016 at 15:20
  • Yes, in some ways, in the option 1. But it doesn't work even if I include all the "from ... import ..." from my gallery.views to my paginator.views with : gallery.views.home(request). Is there a way to make option 2 works ? Maybe by specifying a parameter, like : include("gallery.views.py", home()) ? Commented May 2, 2016 at 15:29
  • You trying something like a redirection? What do you need in gallery.home from custom_page? Commented May 2, 2016 at 15:33
  • All my pages have : name, seo_title, seo_decription,(etc..) and also txt_content and template link... Commented May 2, 2016 at 15:54
  • I don't understand how custom_page view is helping yet. You can use a template_processor to pass variables to every template if you need. Commented May 2, 2016 at 16:23

2 Answers 2

1

If you need a mechanism which is executed before the request is dispatched to a view, I would recommend using a middleware class. You can read more about it in the Django docs.

Another option is to use class based views to create a SEOView which can be inherited by every custom page view of yours. Some example of how it could look like:

from django.views.generic.base import View

class MySeoView(View):
  def dispatch(self, request, *args, **kwargs):
    # some logic for SEO 
    return super().dispatch(request, *args, **kwargs)


class CustomView1(MySeoView):
  def get(self, request, *args, **kwargs):
    # do normal stuff for this page
    return HttpResponse(...)

  def post(self, request, *args, **kwargs):
    # maybe some other logic for posts
    return HttpResponse(...)

To come back to your own options:

If you want to make #1 work, I guess you have to return the result of the view:

...
if url == 'someUrl':
  return gallery.views.home(request)
...
Sign up to request clarification or add additional context in comments.

Comments

0

Firstly I want to thanks Gocht to have sended me on a good way. So that's what I've done :

paginator.views

def custom_page(request, url):
    """
    Vue d'une page statique.
    """

    if url == 'gallery':
        import gallery
        gal = gallery.views.render_gallery(request)
        gal.render()
        gallery_html = gal.rendered_content

    if url == 'xxx':
        ...

    page_to_load = get_object_or_404(Page, url=url)
    template_to_load = "paginator/" + page_to_load.template_name
    return render(request, template_to_load, locals())

gallery.views

from django.template.response import TemplateResponse

from gallery.models import GalleryItem

def render_gallery(request):
    img_to_display = GalleryItem.objects.filter(published=True
                        ).order_by('-date')
    return TemplateResponse(request, 'gallery/gallery.html', locals())

But yes, now, I understand that something like simP will be more clean. Thanks !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.