how to Write in Django SlugField with hebrew - django

i need help how to write Hebrew in SlugField in django models
now i can write only English
something like: hello-world
but i what to write in SlugField = "שלום-עולם"
and the url be like:
www.somsite.com/blog/שלום-עולם
10X

The whole idea of the slugfield is that it converts a given string to only url-safe characters. Hebrew is unicode, which means that it generally is unsafe to use in the url. If you want to override this, just use your article title instead of a slugfield.

Related

New Line on Django admin Text Field

I am trying to create a blog o django where the admin posts blogs from the admin site.
I have given a TextField for the content and now want to give a new line.
I have tried using \n but it doesn't help. The output on the main html page is still the same with \n printing in it. I have also tried the tag and allowed tags=True in my models file. Still the same. All the tags are coming as it is on the html page.
My Django admin form submitted:
The result displayed in my public template:
You should use the template filter linebreaks, that will convert the reals \n (that means the newline in the textarea, not the ones you typed using \ then n) into <br />:
{{ post.content|linebreaks }}
Alternatively, you can use linebreaksbr if you don't want to have the surrounding <p> block of course.
After searching the internet and trying different Django Template Filters, I came across one specific filter, SAFE.
For me, LINEBREAKS filter didn't work, as provided by #Maxime above, but safe did.
Use it like this in your html template file.
{{post.content|safe}}
To have a better understanding of SAFE filter, i suggest reading the documentation.
{{post.content|linebreaks}}
This will make the line in the textbox appear as it is without using \n or \.
{{post.content|linebreaksbr}}
Besides the newline function in your CSS Declaration will work too.

Django-Haystack: Understanding the use of data template in Haystack

I am new to Haystack. I cannot understand why we have to use a template to render it with the text that we want to search. More simple , why we don't have to use something like this?
text = indexes.CharField(document=True, "and here the attributes to search")
UPDATE
To be more specific Let's say that we have an app places an here a model countries.
In the model i want to be searchable from haystack the fields capital and biggest_cities. So in search_indexes.py i put
text = indexes.CharField(document=True, use_template=True )
After make a template in the path search/indexes/places/countries_text.txt
Here i put
{{ object.capital }}
{{ object.biggest_cites }}
Again the question is: why we have to use a template in order to accomplish our goal?
It wouldn't be easier to use something like
text = indexes.CharField(document=Truer, model_attr='capital',model_attr='biggest_cites')
Have you read this Haystack Documentation page http://django-haystack.readthedocs.org/en/latest/searchindex_api.html ?
If you haven't, you must. If you have, read it again.
The SearchIndex API contains valuable fundamentals of how Haystack works on your project. It can also grant you a useful insight of "why you use templates to make your data searchable'.
why we have to use a template in order to accomplish our goal?
From the Haystack Docs:
"...we’re providing use_template=True on the text field. This allows us to use a data template (rather than error prone concatenation) to build the document the search engine will use in searching"
As you can see, we can choose whether use a template or not.
Ps: sorry for the late post; I hope it helps you.

How to create a WordPress like URL naming convention in Django?

I'm a newbie in Django and in WordPress if you create a Post called "hello world" then the URL by default will be like
wordpress.com/2012/07/05/hello-world/
and if you create another post with the same name it will be
wordpress.com/2012/07/05/hello-world-2/
I want to achieve the same in Django and I was thinking to create a sample urlconf like this
(r'^articles/(\d{4})/(\d{2})/(?P<name>\w+)', 'article.views.article_detail')
and in the views break down the name and iterate through all the items and match the name.
But the problem with will be that I won't be able to reference posts dynamically. For e.g. if I was to link the a hello world post I would need to find out how many posts with the same name exist already and then append the additional number to it which is inefficient.
So what's the best way to do this in Django?
See the documentation for Django's {{ url }} template tag. It lets you pass it a view name and parameters, and automatically generates the correct URL for you.
You can take care of appending numbers to each post's name in the function that generates its slug - you could have a look at django-autoslug

Using `ugettext` in a definition of a Django field

In my models.py file I define a field on a model like this:
description = models.CharField(
max_length=40,
default=_('Bla bla bla'),
)
Now, _ is django.utils.translation.ugettext, and I want to use as a default value the Hebrew translation of 'Bla bla bla', which is 'בלה בלה בלה'. (The website is in Hebrew only.) The string is properly translated in the messages file. But when I run the Django admin and create a new object, I see the English 'bla bla bla' on the field. I assume that English is the active language when compiling the models.py module. How can I solve this and make it Hebrew?
I know that one solution would be to forego ugettext and just write Hebrew inside the Python module, but I prefer to avoid that in order to prevent encoding hell.
Try ugettext_lazy
https://docs.djangoproject.com/en/1.3/topics/i18n/internationalization/#lazy-translation
Always use lazy translations in Django
models.
ugettext merely marks strings as translatable. It doesn't do any translation for you, per se. You have to create language files (.mo and .po) and then set the LANGUAGE_CODE setting to match. Read the Django documentation on Internationalization and Localization for more in depth info.

django 'urlize' strings form text just like twitter

heyy there
i want to parse a text,let's name it 'post', and 'urlize' some strings if they contain a particular character, in a particular position.
my 'pseudocode' trial would look like that:
def urlize(post)
for string in post
if string icontains ('#')
url=(r'^searchn/$',
searchn,
name='news_searchn'),
then apply url to the string
return urlize(post)
i want the function to return to me the post with the urlized strings, where necessary (just like twitter does).
i don't understand: how can i parse a text, and search for certain strings?
is there ok to make a function especially for 'urlizing' some strings? The function should return the entire post, no matter if it has such kind of strings.
is there another way Django offers?
Thank you
First, I guess you mostly need this function in templates (you want to present the post "urlized").
There's the built-in template tag urlize, which is used like
{{ value|urlize }}
which takes a string value and returns it with links.
If you want to customize the logic, you can create your own template tag.
EDIT:
To find the # words you can use a simple regex search:
import re
a = "hello #world. foo #bar! #foo_bar"
re.findall("#(\w+)",a)
>> ['world', 'bar', 'foo_bar']