Stackoverflow like tag system form for django? - django

What I am trying to create is a site for resources. Basically, you add resources such as books and videos via links. Now, with any resource site that caters to a variety of resources, you need to tag them in order to understand what kind of resource you are using.
For example, if you make notes on something like Chemistry or key points from a talk on lets say "Django", then these are text documents. Thus you would want them inside a TEXT TAG.
So, when you are making a form for this kind of thing, what form field would you use? For example, by knee-jerk approach is to simple make a text area field, and then separate the different tags via comma. Now, this can be prone to many problems, I'd just like to know what is the best approach to take to solving this problem? Basically, an easy way to validate the data input? Would forms.ChoiceField be the best approach to the problem or is there something else that is superior?

https://www.djangopackages.com/grids/g/tagging/ is your best bet, most specifically https://github.com/alex/django-taggit. If you want to run your own tagging system, take a look at the source code for some ideas.
EDIT: The easiest way to display this in a form would be to use a ModelMultipleChoiceField. This allows you to select multiple tags for a single resource, and handles server-side validation and conversion to the actual Tag instances. However, I think most people would agree this option looks hideous, and it is certainly not user-friendly if there is a large amount of possible tags.
If you're using jQUery, another option is to use Django_select2. This is what I have personally used in a similar situation, and it handles a large amount of possible tags very well. Django_select2 is a thin wrapper around jQuery's Select2 plugin, with a bit of added functionality (most notably the AutoView and AutoModelSelect2Field). This provides a hybrid between a text field and a select list, allowing you to search all tags and easily select multiple tags. See http://ivaynberg.github.io/select2/ for examples of what you can achieve.

Related

What is the best way to fin and use a template in cakephp 3

i'm new in cakephp and I have started with version 3. I want to build a beautifull app and because I'm not good in design, I would really like to use a free template or buy one that I can use within cakephp.
So, I would really appreciate all your propositions and ideas or best practises. The easy way will be the best because I don't have a lot of time with this project. Thank you in advance.
If you don't have a lot of time like you mentioned, the easiest way to go ahead and get started is to paste a lot of the code in your default.ctp layout inside of src/Template/Layout/default.ctp.
You'll notice there are some lines of PHP already in there that are relevant to fetching blocks of css, meta tags, and other bits of code that could potentially exist throughout your project.
Find the main layout of the theme your trying to use - the one that will be consistent across most of the pages. That's the one you'll use for default.ctp. Compare what's already in default.ctp and make the comparable adjustments around the HTML in that document while keeping the important lines of PHP there as well.
For other important pages like a login or registration page, just create a new document for those, like 'login.ctp', then inside the function that loads the page (maybe 'login' inside of UsersController'), change the default layout with this line of code:
$this->viewBuilder()->layout('login'); // without the .ctp ending
This way you can create one-off layouts that don't really match any other page.

Dynamic templates with Django and Ajax

My question is a bit generic in that my problem is a broad one. I have been working with django for some time, and I really want to move more into doing very dynamic web pages where actual page reloads aren't common.
I have read about the different popular javascript frameworks available, and I always feel like I am missing part of the puzzle, particularly in templating.
What are some of the best practices for keeping my templating code as non redundant as possible. I get the impression that a lot of templating logic will make it's way into the JS in addition to my django templates. I want to avoid situations where I am writing templating code in two different places.
For a very basic example, let's say I am writing some template code inside Django for an input field that has a set number of attributes. I have then also written in JS that when I click on a button, another input field of the same type is generated with all the appropriate attributes. In practice this could be a form that takes an arbitrary amount of e-mail addresses. The problem I see is that when I want to change something about that input field, I need to do it in two places.
Is there a particular development paradigm or work flow that I am unaware of? How are issues like this generally avoided?
Recommendations on frameworks would be amazing too!
as you mentioned above:
Use Django Template language. Pass the data from view to template dynamically.
Read Django Template Language documentation.
For JS :
its better to write your js in home.html.... use {% include %} tag for other html

Post-process/filter all textilizable fields in Redmine via plugin?

I am attempting to write a Redmine plugin which further filters textilizable fields (issue description, updates, wiki content, etc.).
I've leafed around the plugin documentation but did not really find a good place to start. The view hooks seem to allow you to inject content at pre-defined injection points. Nothing really stood out toward creating a filter for textilizable content.
The most straightforward way is to hack the code in application_helper.rb and create your own filter function along the same lines of the :parse_inline_attachments, :parse_wiki_links, :parse_redmine_links. However, I do not see a good way of inserting that in a plugin without monkey patching the entirety of textilizable(*args).
Am I missing something obvious here?
One possible way, here: http://www.redmine.org/boards/3/topics/33949
This board topic mentions the caveats associated with view hooks (mentioned above) and offers a workaround or two that lets you change content by overriding views.

Organizing django-tagging tags or assigning properties to tags?

I'd like to categorize my tags. Here's an example of the tags I have now:
css, internet-explorer, firefox, floats
Each of those are separate tags ( 4 in total obviously ). I would like to mark the internet-explorer and firefox tags as browsers. Does django-tagging offer some sort of way for doing this or do I have to manually edit the schema?
I really don't care if I have to either tag tags or add a new column to the tags table, whichever is the easiest way in accordance with the plugin.
Interesting, I came across this problem as well and solved it like this.
I don't want to mess with the django-tagging code because it will be a pain if I wish to upgrade afterwards, so I made a new module called taggingtools. Taggingtools is for grouping tags and autocompletion in the admin interface. For the grouping I made a new model named TagGroup, this model just has a name. (for Example browsers). I also added some functions to return the tags for that group. I then added the tags for Browsers to the Browsers TagGroup. This way I can say I want all the browser tags for a certain database object. It's easy to make this, but if you can wait I can check if I can opensource it so you and others don't have to build it yourself.

Filtering a Class and Subclass in Django

I have a project with an FAQ app. The app has models for FAQ (written by the site authors) and UserFAQ (written by users-- not just a clever name). I want to return all entries, FAQ or UserFAQ that match certain conditions, but I also want to exclude any UserFAQs that don't match a certain criteria. Ideally, it would looks something like:
faqs = FAQ.objects.filter(question__icontains=search).exclude(show_on_site=False)
Where "show_on_site" is a property that only UserFAQ objects have. That doesn't work because the filter craps out on the parent class as it doesn't posses the property. What's the best way of doing this? I came across this snippet, but it seems like overkill for what I want to do.
In your position, absent a need to have two tables, I'd be tempted to have one FAQ model/table with is_user_faq and show_on_site fields.
Sometimes it helps when modeling data to organize it for simple and fast access. While model inheritance has some appeal, I've found it it's often easier to avoid using it.