Django multiple many to many fields? - django

I am building a news app for my website. I want to use a sort of tag system. Each news article can have different and multiple tags. All tags are saved in a tag model, and i want to connect the tags to the newsarticle. Now is this possible with: tags = models.ForeignKey( TagsModel ) for one tag, but how i can do this with multiple of them?
Thank you!

Use django tagging.
In your model you do: tags = TagField() and presto, you have tags that behave like you expect. The app also comes with several niceties to perform common tasks. e.g. parse input into tags or output the tags in templates.
In general, though, you can have ManyToMany fields like so:
some_things = models.ManyToManyField(OtherModel)
here are the docs for that. You can have multiple ManyToManyFields in a single model, you just need to specify related names.

Related

how to create new tag or select from current tags using Django forms

I have 3 tables, Products, Articles, and tags.
products and articles both have a "tags" field which has ManyToMany relation with the "tags" table
I want to be able to reference multiple tags or create new tags while adding a product or an article (in one field)(using Django forms)
for example, "test1" and "test2" are already in the tags table, I want the field to be like this:
1.I type "te" in the tags field
2.A drop down with "test1" and "test2" is opened which I can choose each one of them to be added in the field
3.I type "test3" (which isn't already in the tags table)and hit enter and "test3" is added to the field
products and articles both have a "tags" field which has ManyToMany relation with the "tags" table
(just like tags in a post in StackOverflow)
I think Django built-in forms can handle this but I'm overwhelmed by the documentation and lost in the configuration of my Django forms.
what kind of fields and widgets should I use for this purpose?
Excuse me if the question is vague I'm new to Django and I would appreciate any kind of help.
I figured it out. I did it with a combination of an Ajax call and its endpoint for searching in the current tags plus some JS to get the JSON data and put it in the field and finally in the Views file I implemented some logic for this field of the form so the data would be saved in the correct normalized format after the form submission.

Can we enter html code in a field in models in django?

I am making a small static website in which I have a template in which I tend to show the privacy policy terms of usage etc. I currently don't have any matter for it and tend to add it in future after deploying the site on server. I wanted to know that if I can in future add the matter on that page through a model i.e I create a model with two fields privacy policy , terms and in and pass it to the template as context in views.py . But I have a concern that the fields will have several headings which I will have to display in bold , so is there any way that I can pass html tags in model field and when I render it in my template as {{privacy}} the part I want in bold or any other style comes as that style.
So is there any way that I can pass html tags in model field and when I render it in my template as {{privacy}} the part I want in bold or any other style comes as that style.
Yes. You only need to tell the Django template engine not to escape the characters (for example translate < to <). You can do this with the |safe template tag [Django-doc]:
{{ privacy|safe }}

Illustrated texts in Django model

I am trying to make one Blog using Django 2.0 and I have already created a primitive one. It has a Post model which is as follows:
class Post(models.Model):
PriKey = models.CharField(max_length=255,primary_key=True)
Heading = models.CharField(max_length=100)
DateOfPost = models.DateField(default=datetime.date.today())
Content = models.TextField()
As it can be seen, the content area is only textual and as of now, I can't add any special style or pictures inside my content.
I thought of using HTML tags inside the text content but they are appearing unchanged when the web page is rendered.
So my question is, is there any way of storing pictures along with the text in the content field of the Post model? I want to make something like this
Is there any way of showing the pictures in their respective positions using Django model? If no, is there any other way of doing this?
Also, is there any way of storing HTML codes inside django models and render them as it is when the website is run?
You can store html tags inside the field.
while rendering, to template mark it as safe
{{ post.content|safe }}
This will render all the html tags.
But this is not a good way as it makes you vullerable to cross site scripting attacks
A better method is to use something like a ckeditor
It provides a RichTextField and RichTextUploading Field and using this you can upload pictures, videos, code snippets, style your text and a lot more inside one field.
There are many other optons, but I prefer ckeditor
Ckeditor is a cross platform editor, django-ckeditor is a library containing django implementation of ckeditor which gives you full backend and frontend combined
ckeditor
django-ckeditor
django-pagedown A django app that allows the easy addition of Stack Overflow's "PageDown" markdown editor to a django form field, whether in a custom app or the Django Admin
I think you should give it a try
Cheers :)

'Hiding' form query from URL (Django 1.3)

I have a form with 6-7 fields. After user input, my webapp searches for those fields in a database and displays the results.
Now the issue is, that the URL ends up having all the form field names and their values in it.
result/?name=lorem&class=arc&course=ipsum
Now with the form having 7-8 fields the url ends up looking ugly.
Is there a Django technique to 'hide' these from the URL? Quotes around hide because I'd be okay with a completely different way to pass the objects to my database from the form as well.
Use a POST request. Here's the django docs on forms and a specific example using POST>. HTML-wise, all you need to do is change the method on the form tag.
I do not recommend to use POST requests for search. If you'll use GET it will be easer for user, he can just bookmark a link and save search or share search results with friends.

How to display related tags in django tagging?

i am using django tagging. can anyone give any example as to how can i show the related tags, when the object related to a particular tag is displayed? Something like similar tags in stackoverflow.
Thank you!
You can use the get_related manager which will:
Retrieve a list of instances of the specified model which share
tags with the model instance obj ordered by the number of
shared tags in descending order.
To use this you could create a template tag such as:
#register.inclusion_tag(your_template)
def related_objects(object, limit=3):
objects = TaggedItem.objects.get_related(object,object.__class__)
return {'objects': objects[:limit]}
Edit for comment
to get a list of similar tags you can use related_for_model, which will return "other tags used by items which have all the given tags"