What's 'slug' for in Django? [duplicate] - django

This question already has answers here:
What is a "slug" in Django?
(13 answers)
Closed 5 years ago.
In Django generic views, there's slug_field, and slug_url_kwarg.
In this context, what is the definition of slug?
I choose the more persuasive explanation within items of 3 dictionaries.
In Cambridge dictionary:
A piece of metal used instead of a coin for putting in machines
In MW:
A disk for insertion in a slot machine; especially :one used illegally instead of a coin
In Oxford:
A part of a URL which identifies a particular page on a website in a form readable by users.
They don't seem to make sense.

It is from the publishing world from wikipedia:
In newspaper editing, a slug is a short name given to an article that
is in production. The story is labeled with its slug as it makes its
way from the reporter through the editorial process. The AP Stylebook
prescribes its use by wire reporters (in a "keyword slugline") as
follows: "The keyword or slug (sometimes more than one word) clearly
indicates the content of the story."[1] Sometimes a slug also contains
code information that tells editors specific information about the
story — for example, the letters "AM" at the beginning of a slug on a
wire story tell editors that the story is meant for morning papers,
while the letters "CX" indicate that the story is a correction to an
earlier story.[2][3] In the production process of print
advertisements, a slug or slug line, refers to the "name" of a
particular advertisement. Advertisements usually have several markers,
ad numbers or job numbers and slug lines. Usually the slug references
the offer or headline and is used to differentiate between different
ad runs.
From there, the slug for web publishing was born as an effort to make more semantic URLs. This is the slug as used in django:
Some systems define a slug as the part of a URL that identifies a page
in human-readable keywords.[4][5] It is usually the end part of the
URL, which can be interpreted as the name of the resource, similar to
the basename in a filename or the title of a page. The name is based
on the use of the word slug in the news media to indicate a short name
given to an article for internal use. Slugs are typically generated
automatically from a page title but can also be entered or altered
manually, so that while the page title remains designed for display
and human readability, its slug may be optimized for brevity or for
consumption by search engines. Long page titles may also be truncated
to keep the final URL to a reasonable length. Slugs are generally
entirely lowercase, with accented characters replaced by letters from
the English alphabet and whitespace characters replaced by a dash or
an underscore to avoid being encoded. Punctuation marks are generally
removed, and some also remove short, common words such as
conjunctions. For example:
Original title: This, That and the Other! An Outré Collection
Generated slug: this-that-other-outre-collection
Django provides a slug field, and in its documentation provides a definition as well:
Slug is a newspaper term. A slug is a short label for something,
containing only letters, numbers, underscores or hyphens. They’re
generally used in URLs.

Related

What is the difference between SlugField() vs CharField() in Django models

So, in my Django projects, I made my model to be like the following
class Store(models.Model):
domainKey = models.CharField()
I had the above to make each store has its own domain like the following
www.domain.com/my-name-is-django
Anyway, it was perfectly working fine. But, I just found out SlugField() which is used for the same purpose as what I did in above.
My question is why we need to use SlugField() because I implemented the same thing without SlugField(). Is there its own any feature that CharField() doesn't have?
A slug is a string without special characters, in lowercase letters and with dashes instead of spaces, optimal to be used in URLs. An example of slug could be:
example/this-is-a-slug/150
you can look for more information here Documentation django slug
CharField has max_length of 255 characters , and accept special characters.
About CharField Here

How to compare and substitute strings in Ruby on Rails?

Trying to build a barebones concept in Ruby on Rails that will take a string, map each individual word in this string, compare it and then substitute the word if it matches predefined strings in related databases.
For example: User inputs in text field "What does lol and brb mean?" Hits Submit button. The action gives back the same text with "lol" and "brb" changed to "laughing out loud" and "be right back".
So far I have a Post model & table for the User input that stores the string in the database.
I have an Acronym model & table that has "lol" and "brb" stored in database with a foreign key reference to Acronym_Translate model & table that has "laughing out loud" and "be right back" referenced to "lol" and "brb", respectively.
How would I connect the Post model/table to the Acronym model/table in order to compare the strings in Post and substitute with strings from Acronym model/table? And what command could achieve such function? Would gsub! method work here?
Any help would be appreciated!
Are you sure that you want to connect the Post table to the Acronym table? This means that you would have to identify and keep a record of each instance of an acronym within a post.
You can do this using a many to many relation or if you want to store extra data about each acronym occurrence you should create a link table named AcronymPost and use a has many through relationship between Post and Acronym. When you parse a post value, and when you identify an acronym in the post, you would have to record this in the database and then use gsub to replace the post value with the acronym.
You can iterate through your table of acronyms and use (string).include? method to check if it occurs in the post. Finally, you could use a gsub command to replace the acronym with its translation.

To Use Django-Haystack or not?

So this might be an obvious answer to some but I'm not sure what the right answer is. I have a simple donation application where Donor objects get created through a form. A feature to be added is to allow of a search for each Donor by last name and or phone number.
Is this a good case to use django-haystack or should I just create my own filters? The problem I may see with haystack is that a few donations are being submitted every minute so could indexing be a problem? There are currently around 130,000 records and growing. I have started to implement haystack but have realized it might not be necessary?
Don't use haystack -- that's for fast full-text search when the underlying relational database can't handle it easily. The use case for haystack is when you store many large documents with huge chunks of text that you want indexed by words in the document so you can easily search.
Django by default already allows you to easily index/search text records. For example, using the admin backend simply specify search fields and you can easily search for name or telephone number. (And it will generally do case insensitive contains searches -- this will find partial matches; e.g., the name "John Doe" will come up if you search for just "doe" or "ohn").
So if your models.py has:
class Donor(models.Model):
name = models.CharField(max_length=50)
phone = models.CharField(max_length=15)
and an admin.py with:
from django.contrib import admin
from mysite.myapp.models import Donor
class DonorAdmin(admin.ModelAdmin):
model = Donor
search_fields = ['name', 'phone']
admin.site.register(Donor, DonorAdmin)
it should work fine. If an improvement is needed consider adding an full-text index to the underlying RDBMS. For example, with postgres you can create either a text search indexes post 8.3 with a one liner in the underlying database, which django should automatically use: http://www.postgresql.org/docs/8.3/static/textsearch-indexes.html

Sensible URL patterns in Django-nonrel with MongoDB

A common practice in many news sites is to include both ID and slug in the URL. The ID is used to look up the actual article, and the slug is included for SEO purposes. This way, the slug can be changed to match a change in the article title without rendering useless any previous bookmarks.
Using the MongoDB ObjectId in URLs is cumbersome as it creates very very long URLs (http://www.mysite.com/article-504119a051e2726c9aa28ea1/my-article-title.html) - is there a better solution??
You do not have to use MongoDB's default ObjectID if there is a more suitable choice for your use case. For example, you can define a custom _id field using a shorter value such as timestamp or perhaps an incrementing counter (see: How to make an auto-incrementing id field). If your use case is publishing articles and there aren't hundreds every minute, you could probably get reasonable uniqueness for an _id with a unix timestamp concatenated with a random value.
If your slugs are unique (or you could accept this restriction), you could potentially use the slug as the _id for even shorter urls. The caveat on _ids is that they cannot change, so a separately indexed slug field will give you more flexibility.
Given your goal of using slugs for SEO, you probably want to add some finesse so that there is a 302 redirect to the current "canonical url" (with correct slug field) if an alternative slug is provided. Otherwise you may incur potential SEO penalties for duplicate content if only the id portion of the url is checked.

MediaWiki Template Templates?

Not sure I have the correct terminology here. I'll explain what I want to do and you guys can tell me if it's possible.
I'm using MediaWiki as a Customer List page. So, I have a category for customers, and for instance, I have 20 customers. Inside the actual customer page I have several "headings" that make up the customer page, including an infobox. What I'm wanting to know is, how I would go about "including" the headings as a template "Customer Landing Page". Meaning, each "Customer Landing Page" (Customer A or Customer B, etc, etc) has the same "headings", but not the same content - so all I want is that each customer page, I can include a "template" and it has the same headings with no content under the headings - so that each time I change this "template" file, it changes it on every customer, and all I have to do is edit the content on the customer page that is required.
You'll have to make one big template for the entire customer page, in which you put all the info. I'll make an example template for a page with two headers, "Customer Landing Page" and "More info". The headers are fixed, and the contents below it vary between customer page.
First, you make the template by creating the page Template:Customer
In here you put:
=Customer Landing Page=
{{{landingpagetext}}}
=More info=
{{{moreinfotext}}}
The triple accolades indicate the variables you will later define in each customer page. For customer A:
{{customer
| landingpagetext = This is the landingpage for customer A
| moreinfotext = This customer is a vegetarian
}}
Customer B:
{{customer
| landingpagetext = This is the landingpage for customer B
| moreinfotext = This customer likes Tom & Jerry
}}
The double accolades indicate the start of a template, and the first word is the templatename used. Then after each pipe ( | ) you can assign variables. I only used newlines to make it easier to read, you don't have to do that (but it makes it easier to maintain).
If you don't use the variable names (like {{customer|Landing page text|More info text}} ) you can access the variables by the order they are defined in, using {{{1}}} and {{{2}}} in the template.
If the customer pages are really big you might want to split the template up and use one per section.
Another option (but more complex) is the use of Navboxes. This requires a lot more set up but mayb be closer to what you are looking for?
You could look at using MultiBoilerPlate, I use this to set default text in pages. I would call this a template but Mediawiki uses that term for something else. If you just want to load the same default text when you start a new page and then fill it in with your own text, then I think this is what you need.