How to create a unique slug in Django

I use this snippet for generating unique slug and my typical save method look like below slug will be Django SlugField with blank=True but enforce slug in save method. typical save method for Need model might look below def save(self, **kwargs): slug_str = “%s %s” % (self.title, self.us_zip) unique_slugify(self, slug_str) super(Need, self).save(**kwargs) and this will … Read more

django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category__new.slug

The reason for this constrain could be that you didn’t have any field called slug in Category class when you have initially migrated it (First Migration), and after adding this field in the model, when you ran makemigrations, you have set default value to something static value(i.e None or ” etc), and which broke the … Read more

Convert non-ASCII characters (umlauts, accents…) to their closest ASCII equivalent (for slug creation)

The easiest way I’ve found: var str = “Rånades på Skyttis i Ö-vik”; var combining = /[\u0300-\u036F]/g; console.log(str.normalize(‘NFKD’).replace(combining, ”)); For reference see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize

How can I create a friendly URL in ASP.NET MVC?

There are two steps to solve this problem. First, create a new route or change the default route to accept an additional parameter: routes.MapRoute( “Default”, // Route name “{controller}/{action}/{id}/{ignoreThisBit}”, new { controller = “Home”, action = “Index”, id = “”, ignoreThisBit = “”} // Parameter defaults ) Now you can type whatever you want to … Read more

Can an “SEO Friendly” url contain a unique ID?

Be careful with allowing a page to render using the same method as Stack overflow. http://stackoverflow.com/questions/820493/random-text-can-cause-problems Black hats can this to cause duplicate content penalty for long tail competitors (trust me). Here are two things you can do to protect yourself from this. HTTP 301 redirect any inbound display url that matches your ID but … Read more

Replace all characters that aren’t letters and numbers with a hyphen [duplicate]

This should do what you’re looking for: function clean($string) { $string = str_replace(‘ ‘, ‘-‘, $string); // Replaces all spaces with hyphens. return preg_replace(‘/[^A-Za-z0-9\-]/’, ”, $string); // Removes special chars. } Usage: echo clean(‘a|”bc!@£de^&$f g’); Will output: abcdef-g Edit: Hey, just a quick question, how can I prevent multiple hyphens from being next to each … Read more

Why SlugField() in Django?

Why SlugField() in Django? Because: it’s human friendly (eg. /blog/ instead of /1/). it’s good SEO to create consistency in title, heading and URL. The big disadvantage of your dynamically generated slug, accepting slugs in urls.py and not using the slug to get the right object? It’s bad design. If you supply and accept slugs, … Read more

How to make Django slugify work properly with Unicode strings?

There is a python package called unidecode that I’ve adopted for the askbot Q&A forum, it works well for the latin-based alphabets and even looks reasonable for greek: >>> import unidecode >>> from unidecode import unidecode >>> unidecode(u’διακριτικός’) ‘diakritikos’ It does something weird with asian languages: >>> unidecode(u’影師嗎’) ‘Ying Shi Ma ‘ >>> Does this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)