I want to make contact page is made of simple template html.
in view.py i made this
from .models import tutorials
from django.views import generic
class Contactviews(generic.View):
template_name = 'home/contact.html'
and this is my url:
url(r'^contact$',views.Contactviews.as_view(), name='contact')
the contact.html is just a simple text that extends from base.html .and it doesnt involve any functionality from models.py
so the page is not rendering.
i think maybe problem is on my url address.
The base View class doesn't know anything about templates. You should use TemplateView.
Related
This is a basic question but I can't figure out what I'm doing wrong...
I'm trying to render a detail view in my django site, but my calls to get the object are just failing or else not rendering.
Here is what I have:
views.py
from django.template import loader
from django.views.generic.base import TemplateView
from django.http import HttpResponse
from .models import user_profile
def detail(request, content_id):
template = loader.get_template('profiles/detail.html')
profile = user_profile.objects.get(pk=content_id)
context = {'profile': profile}
return HttpResponse(template.render(context, request))
Within the template, I have simply been testing the calls using:
<h1>{{ profile }}</h1>
<h1>{{ profile.name }}</h1>
This is my first time rendering this from scratch, I know I'm missing something dumb I just can't sort it. Thank you!
edit This current setup receives a 500 error. Without the get(pk) statement it loads, but doesn't show the variables, just the rest of my HTML.
upon request, here is the urls.py:
urlpatterns=[
path('<int:content_id>/', views.detail, name='detail'),
]
edit: solution Solved! Ends up there was an issue with the model where a migration was not properly performed and a column was read as missing.
Model was contained a column that was missing in the database. Even though it was not being called, it still resulted in an error when the model was referenced.
I have followed http://neutronx.github.io/django-markdownx/js/docs/markdownx.html#MarkdownX docs but can't get it done properly.
What is the correct way to setup two or more editors in the same page?
You don't have to set it up that way. MarkdownX is already initiated as your load {{form}} and {{form.media}}, so it has no meaning. Now, coming to your question. Using two editors on the same page in really straight forward.
in your forms.py:
from django import forms
from markdownx.fields import MarkdownxFormField
class FirstForm(forms.Form):
yourfirstfield = MarkdownxFormField()
class SecondForm(forms.Form):
yoursecondfield = MarkdownxFormField()
in your views.py:
from django.shortcuts import render
from .forms import FirstForm, SecondForm
def form_view(request):
context = {
'first_form': FirstForm,
'second_form': SecondForm
}
return render(request, 'form_template.html', context)
in your form_template.html:
<form>
<p>{{first_form}}</p>
<p>{{second_form}}</p>
</form>
I hope that helps!
I'm using wagtail without its templates (I built an API using rest-framework).
I would like to change the format of the images in the rich text editor
For example this is my RichText field right now:
<p>test test test</p>
<p><br/></p><p><embed alt=\"IMG_1232.jpg\" embedtype=\"image\" format=\"test\" id=\"4\"/><br/></p>"
Instead I would like it to include only the direct link to the image, and even better to the image with the filter I defined (with register_image_format).
e.g.:
<p>test test test</p>
<p><br/></p><p><embed href="/media/IMG_1232.width-400"/><br/></p>"
Is it possible?
I looked into hallo.js but not sure what to do with it...
Thanks
Add a method to your model which returns the result of calling the richtext filter on your rich text field. You can then use this method in place of the field itself within the api_fields definition:
from wagtail.core.templatetags import wagtailcore_tags
def MyPage(Page):
body = RichTextField()
def rendered_body(self):
return wagtailcore_tags.richtext(self.body)
api_fields = [
APIField('rendered_body'),
]
To get the full URL for images, embed and documents, you have to run the content of the richtext block through the richtext filter template tag.
from wagtail.wagtailcore.templatetags import wagtailcore_tags
rich_text = wagtailcore_tags.richtext(rich_text_source)
rich_text should have embed and images with full URL.
Using with DRF it would look something like this, if your doing it in your serializer
from django.contrib.auth.models import User
from django.utils.timezone import now
from rest_framework import serializers
from wagtail.wagtailcore.templatetags import wagtailcore_tags
class SomeSerializer(serializers.Serializer):
rich_text = serializers.SerializerMethodField()
def get_rich_text(self, obj):
return wagtailcore_tags.richtext(obj.rich_text.source)
I am an absolute beginner in django-cms, just acquired some pieces of knowledge to create templates. Just wondering, how to create a portal page that has a few acticles in each different categories?
Please simply point out a practical way to do, no real code is needed.
Thank you.
As others have pointed out, the way to do this is by hooking your CMS page to another set of views. Django-CMS provides application hooks:
#cms_app.py
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
class YourModuleApp(CMSApp):
name = 'My App MOdule'
urls = ['my_app.urls']
apphook_pool.register(YourModuleApp)
So, if you had a module called "my_app" with a urls.py in it, Django-CMS will add those patterns to the page. Look in the "Advanced Settings" section of the page in admin for the application drop-down menu.
Once the app is hooked to the page, Django-CMS will pull any content and the layout template from the information it holds, then hand off processing to the additional URL patterns that are hooked to it. That's how you can pull in another model, add a form, handle a POST, etc.
You could just do it the normal Django way. Create a normal Django app, with a URL pointing to a view that renders a template. The view could look like this:
from django.shortcuts import render
from cms.models import Page
def articles(request):
pages = Page.objects.public()
render(request, 'example.html', {'pages': pages})
And the template could look like this:
{% load cms_tags %}
{% for page in pages %}
<p>{% page_attribute "page_title" page %}</p>
{% endfor %}
You could stop here. Or you could have...
Additional Django CMS integration with AppHooks
Do you want your non-developer content managers to be able to put a list of articles wherever they want? This is where AppHooks come in.
Create a CMSApp class in the file appname/cms_app.py like this:
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
class ArticleListApp(CMSApp):
app_name = 'articlelist'
name = _('Article List')
def get_urls(self, page=None, language=None, **kwargs):
return ['articlelist.urls']
apphook_pool.register(YourModuleApp)
Delete the URL entry in your project-wide urls.py as you no longer need it. Your app's urls.py needs to include a view for the regex ^$.
Now you or any content manager user with necessary permissions can create a page in the admin interface, and modify the advanced settings to select the "Article List" application:
One gotcha is that this will have no effect until the page is published (as well as all of its ancestor pages).
Hi I need to refresh my custom template tag --right_side.py-- via Ajax. Is there a way to import the template tag in the view and return it as HttpResponse because I don't want to give up my custom template tag (it works great in other pages) nor code a new view action which is really similar to it.
Having a link to call with Ajax or loading it in the view inside
if request.isAjax():
Are both fine for me.
I find it really useful when refreshing an area with ajax. So thought it would be good to share it:
First you import the custom template tag you coded in your view file.
from your_app_name.templatetags import your_tag_name
And then you use it like this:
return HttpResponse(your_tag_name.your_method(context))
That worked for me and I got the template tag as response from server and refreshed the div with that result.
I had this same question awhile ago, I was loading HTML snippets with AJAX which I had already written as template tags. And I was trying to avoid duplicating the code in two places.
This is what I came up with to render a template tag from a view (called via ajax):
from django.template import RequestContext, Template
def myview(req):
context = RequestContext({'somearg':"FooBarBaz"})
template_string = """
{% load my_tag from tagsandfilters %}
{% my_tag somearg %}
"""
t = Template(template_string)
return HttpResponse(t.render(context))
You could create a template just containing your templatetag and nothing else. Then you would have in right_side.html:
{%load cems_templatetags%}
{%right_side%}
and in the view something like:
if request.isAjax():
return render_to_response('right_side.html',RequestContext(request))
I found this solution :
from templatetags_file_name import my_templatetag
return render (request,'path/to/template.html', my_templatetag(parameter) )
And, in my "templatetags_file_name", "my_templatetag" is like that :
#register.inclusion_tag('path/to/template.html')
def my_templatetag(parameter):
#my operations
return locals()