Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying external application listview and detailview #140

Closed
sunjoomoon opened this issue Mar 29, 2016 · 3 comments
Closed

Displaying external application listview and detailview #140

sunjoomoon opened this issue Mar 29, 2016 · 3 comments

Comments

@sunjoomoon
Copy link

Trying out https://pythonhosted.org/django-page-cms/display-content.html#display-content-from-other-applications, but I got stuck.

If I have an app, called news with model of News and want to display like below

# news.models.py
class News(models.Model):
    title = models.CharField(max_length=200)
    postdate = models.DateTimeField(default=datetime.now)
    body = models.CharField(max_length=200)

and put below in the default template, for example

# templates/pages/default-template.html
<ul>
{% for new in news %}
    <li>
        <h2>{{ news.title }}</p>
        <p>{{ news.body }}</p>
    </li>
{% endfor %}
</ul>

Put below in news.views.py

#news.views.py
..........
def extra_context():
    from news.models import News
    lastest_news = News.object.all()
    return {'news': lastest_news}

PAGE_EXTRA_CONTEXT = extra_context

In the browser, nothing is rendered. What am I missing? Normally I would tend to have models, admin(and/or forms), views, urls, templates.

@batiste
Copy link
Owner

batiste commented Mar 31, 2016

Hi, most things looks correct. But just so you know PAGE_EXTRA_CONTEXT is a setting so you should put that in your settings.py file otherwise this will not work.

Alternatively you can subclass the CMS view and use you own instance of it which is probably the preferred and most flexible solution:

https://github.com/batiste/django-page-cms/blob/master/pages/views.py#L168

Just sublass the Details class based view redefine the extra_context method and use an instance of it your urls.py.

@sunjoomoon
Copy link
Author

In my settings.py (project), I put below

# https://github.com/batiste/django-page-cms/issues/140
PAGE_EXTRA_CONTEXT = extra_context

Then again, browser shows below error

  File "./settings/settings.py", line 366, in <module>
    PAGE_EXTRA_CONTEXT = extra_context
NameError: name 'extra_context' is not defined

Do we have one complete example? As a newbie, working example would make more sense to me, if not all too troublesome.

@batiste
Copy link
Owner

batiste commented Apr 6, 2016

Well the error is obvious, extra_context doesn't exist within the context of your settings. Add your extra_context function inside your settings (with the necessary import inside the function itself).

But this is quick and dirty solution. I strongly recommend you to subclass the Detail view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants