I have one app in my django project.
I created seo.py file and add:
from rollyourown import seo
class MyMetadata(seo.Metadata):
title = seo.Tag(head=True, max_length=68)
description = seo.MetaTag(max_length=155)
keywords = seo.KeywordTag()
class HelpText:
title = "This will appear in the window/tab name, as well as in search results."
keywords = "A comma separated list of words or phrases that describe the content"
description = "This will appear in the description"
class Meta:
seo_views = ('app_name', )
in my base.html in head I added:
{% load seo %}
{% get_metadata %}
but nothing is displayed. What is wrong? (Of course, I added data in the admin panel - My metadatas (View))
Loading a template tag library will not work if you haven't put that application in your INSTALLED_APPS variable in settings.py. Have you put rollyourown.seo in there?
The paths you need to set up should be in the format '/pagename/'. For your homepage you just need '/'.
#ringfirebug, probably you already solved your issue, but I will answer.
You don't need to set Path for each page. Use Model, Model Instance or a view. From your configuration I see that you have still a default view in seo_views. It should look like this to start working:
class Meta:
seo_views = ('your_app_name.view_name',)
seo_models = ('your_model_1', 'your_model_2',)
So, when creating a new Metadata entry in Models or Views you will be able to see these models and / or views in the select box.
Next, in your template, if it still doesn't work, you have to get seo data exactly for this object,
this worked for me.
{% load seo %}
{% get_metadata for obj as metadata %}
{% metadata %}
Here 'obj' is the object, for example you product that you path to the template through your view by RequestContext.
Let me know if you managed to solve it.
By the way, if you already have managed to use external data in your metatags, like "Best {{ product.name }} of the year", let me know how.
Related
I am making a catalog of film directors for an introduction to django exercise and im trying to load the images from my database to my template but is not working.
All my steps:
I added an imagefield to the directors model class, which I have then filled in from the admin version.
Then in my view I have made a request to collect a list of all directors like so:
directors = Directors.objects.all()
and then I have returned it with render.
In the template I have done this
{%for director in directors%}
<img src="{{director.director_image.url}}" alt="{{director.first_name}}">
{% endfor %}
And everything is fine except that the images are not loaded, the alt with the names of the directors are ok :D (an advance). I get the feeling that somehow I have to define the url of the images??? because from the browser console it get the url of the images properly but it gives a 404.
im really on my first days using django so I guess im missing something with how the media urls works.
hope someone can help me. tysm <3
What I tried?
I haven't tried anything else since I'm very new to django. I have searched in google how images are treated and I have read that a root is necessary for your media? I simply added the imagefield to my model and in the admin view I uploaded it but I have not done anything else.
What I want?
I want to show the images of all the directors in my template
You mispelled directors spelling in template.
change this:
{%for director in directors%} #According to view, you mispelled directors spelling here
<img src="{{director.director_image.url}}" alt="{{director.first_name}}">
{% endfor %}
To this:
{%for director in directores%} #Here you did mistake
<img src="{{director.director_image.url}}" alt="{{director.first_name}}">
{% endfor %}
Now your problem is solved.
In the Django CMS there's the {% placeholder 'content' %}. I tried to use it on a non-django-cms page, i.e., a detail-view page that comes from an apphook. However, when I switch to the structure view in the detail-view page and the placeholder does not seem to reflect. Is that's how it's supposed to work or is there a problem with my code? If it's how it's supposed to work is there a way to make placeholder appear in the page?
You can't use {% placeholder outside of CMS pages.
If you're on one of these pages, you can use a static placeholder. These will show the same content on any page where a static placeholder with the same name exists. So a good example of these is a footer, or header where you'd want it to be the same on all pages;
{% static_placeholder "footer" %}
Another thing you can use, good for your example of a detail page in an apphook, is a PlaceholderField on your models.
Take this example;
from django.db import models
from cms.models.fields import PlaceholderField
class Category(models.Model):
name = models.CharField(max_length=20)
description = PlaceholderField('category_description')
In your template you can then render this placeholder and it'll behave like a standard placeholder on a cms page;
{% load cms_tags %}
{% render_placeholder category_instance.description language 'en' %}
You can find docs for PlaceholderField here
I'm looking for a way to lowercase the first letter of a model in my django admin site.
i.e.:
model verbose name is "agent-1.0.0" is shown as "Agent-1.0.0" on the dashboard,
simple but IDK
grappelli trick will also work for me.
django - 1.7.1
also - need this only for one app models group - not all of my dashboard should be lowercase...
so, overriding the index.html is not so efficient
The capitalization is hard-coded in the template, same for the templates in Grappelli.
You can use catavaran's suggestion, but this will transform every model name. Overriding the template is a huge pain in the ass to maintain for something this small.
The only workable solution I can think of is to bypass the capfirst filter with a space:
class Meta:
verbose_name = " agent-1.0.0"
As capfirst only forcibly capitalizes the first character, nothing will happen if the first character is not a letter.
Model name passed to template as capfirst(model._meta.verbose_name_plural) so you have to lowercase it in the admin/index.html tempate or via CSS. Imho CSS option is simpler:
div.module tr[class^=model-] th {
text-transform: lowercase;
}
If you want lowercase only some models (for example User) then change CSS selector to this:
div.module tr.model-user th {
text-transform: lowercase;
}
With Grapelli you could create a custom Dashboard by running:
python manage.py customdashboard
and setting GRAPPELLI_INDEX_DASHBOARD on your settings to your custom class.
You can make this custom class extend from the Dashboard class that grappelli offers and override it to your needs. Look especially at the ModelList class, where you can specify the title you want for the model.
There is a CSS-way for those who don't want to override Django admin classes. Override and extend templates/admin/base_site.html template as follows:
{% extends "admin/base_site.html" %}
{% block extrahead %}
<style>
h1.model-title {text-transform: lowercase;}
h1.model-title:first-letter {text-transform: uppercase;}
</style>
{% endblock %}
{% block content_title %}
{% if title %}<h1 class="model-title">{{ title }}</h1>{% endif %}
{% endblock %}
This will make only first letter of each content_title uppercase.
You can use the same way to lowercase model name in admin tables as well as sidebar. However, I'd like to point that by tacit agreement model's verbose_name as well as verbose_name_plural shouldn't be capitalized. This will save you a lot of overrides in your project, like I provided above to normalize change_list header.
I use mongoengine, and love it very.
now I work it with django, and I get a problem with it
how to set property for ListField
e.g.
this was my model
class Bussiness(Document):
tags = ListField(StringField())
and I want to use it on template like this:
{% for tag on bussiness.tags %}
{{ tag.url }}
{% endfor %}
but the tag.url I don't want to save it to database, just hope to produce it on model level, and make tags only some string on database.
And if I hard code it on template, I have to write it everywhere, that was I disgust.
This wont work as tag is just a string (you have it defined as a StringField).
You could have tag as an embedded document with url being a property eg:
class Tag(EmbeddedDocument):
name = StringField()
#property
def url(self):
return "http://my-ace-site.come/businesses/%s/"
class Business(Document):
tags = ListField(StringField())
Hey im using the placeholder fields from django cms in some of my custom cms apps. Basically what im trying to achieve is specific styles and filters for the same placeholder fields being used in different templates.
for example if i have a model for vacancies that looks something like this:
from django.db import models
from cms.models.fields import PlaceholderField
# Create your models here.
class Vaccancy(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique = True)
ref_number = models.CharField(max_length=255)
info = PlaceholderField('info')
active = models.BooleanField(default=True, verbose_name="posistion active?")
and another model that also utilizes the placeholder field in a similar way. What i hoped i could do is overide the tex.html template then have some conditional logic to detect the name of the placeholder like so
{% ifequal placeholder "info" %}
{{ body|truncatewords:200|safe }}
{% endifequal %} the aim of this is so i can specify different filters like truncatewords etc as i dont want to apply this to every placeholder that uses a text plugin!
hope that was clear enough! cheers for any help!
If you use placeholder fields, you have to check for placeholder.slot, also note that {% if placeholder.slot == "info" %} seems a bit nicer than ifequal :D