How to link tags to search results in a Django Blog? - django

I am building a blog in django and am using django-taggit. I'm trying to figure out how to tie the link of a tag to a search result page that shows all of the posts using that tag.
I already have the search page created, I know how to filter queries to the page to bring the correct results, and know how to link to the page itself {% url 'search' %}. But how would I pass queries to the page from the template?
For instance, if I have a post tagged "dog" I want users to be able to click on the tag "dog" and be taken to the search page that only has results for posts also tagged "dog".
The django documentation for class views does not have examples of this. And every tutorial resource so far has been focused on the filtering and displaying of the search page itself rather than linking to it with desired queries in an <a> tag instead of a <form>.
In short, how do you make an <a> link pass a query into a url, like how an <input> would to a <form> action in Django?

I found the answer. Based on this , it is
<a href="{% url 'myview' %}?q=foobar">

Related

Use template tag (in HTML ) in view as a variable

I'm creating a web page on Django,
I want to create next and previous post in the bottom of blog page.
is there any way to use a simple tag like {{post.title}} in HTML and refer it to view page to find the index of current post ?
This is where you should use pagination. If you are using DRF you might need this pagination.

Django enable URL link in template

I have a blog and users can comment any articles. For now, when they post an URL, we can not follow the link by a clic. Because there is no tag.
I know we can display HTML by using the filter "|safe". But I don't want a user using all html tag for security reasons.
I just want to make URL like "http://google.fr" as active and cliquable.
Any idea ?
The urlize filter does exactly what you want.

Django redirect to form page and submit the form?

Right now. I have a search function in my page to search for item id. When I click search, I will render the same page with the result items and show item. And in other pages where I also display the item id, I want to add a link to the id to go to the same page where I search for that id.
Example: id: 123, I want the same page when:
1. search '123' in my search page(my search only accept exact match)
2. In other pages, click '123', go to the search page with results
How should I achieve this, I have tried many ways which don't wok.
You need to make use of the GET method that HTML forms provide. When you perform a search from the first page, you must make sure that you are doing so using the GET method in the form. This will append the form data into the URL.
E.g. If you have a 'name' field in your form which has 'John' inputted. The submission of this form will compose a URL like so:
http://someurl.com/?name=John
This can then be accessed using the Django request object:
name = request.GET['name']
You've probably done something similar already for displaying your search results. So, all you need to do is create a link in your second page that redirects to the search page with GET request variables appended.
E.g.
<a href="{% url 'search_page' %}?searchterm=232> Item 232 </a>

How do I access my query when using Haystack/Elasticsearch?

I originally followed this tutorial (https://django-haystack.readthedocs.org/en/latest/tutorial.html), and have so far been able to highlight my query within my returned results. However, I want to highlight this same query when visiting the next page that I load with a separate template. Is there any way to save/access this query so that I can highlight the same results within this other template?
Whenever I try and include a statement like this, I get an error, which I'm thinking is because I'm not trying to access the query properly.
{% highlight section.body with query html_tag "span" css_class "highlighted" %}
You have to send to the next page, the information that you use to highlight the results in the first page. You can use the request.session to store the data and call it in the next page, or you can send the sqs by the url to the next page.
If you want to know how to manage the search query set, and how to edit that kind of stuff, I recommend you to read the views.py forms.py and the elasticsearch_backend in the haystack folder at: "/usr/local/lib/python2.7/dist-packages/haystack"
This is the url for the documentation of Django Session: Django Session
This is the url for the documentation to pass parameters trhough url: URL dispatcher

Putting links in list_detail.object_list to list_detail.object_detail

I've started using Django and am going right to generic views. Great architecture! Well, the documents are great, but for the absolute beginner it is a bit like unix docs, where they make the most sense when you already know what you're doing. I've looked about and cannot find this specifically, which is, how do you set up an object_list template so that you can click on an entry in the rendered screen and get the object_detail?
The following is working. The reason I'm asking is to see if I am taking a reasonable route or is there some better, more Djangoish way to do this?
I've got a model which has a unicode defined so that I can identify my database entries in a human readable form. I want to click on a link in the object_list generated page to get to the object_detail page. I understand that a good way to do this is to create a system where the url for the detail looks like http://www.example.com/xxx/5/ which would call up the detail page for row 5 in the database. So, I just came up with the following, and my question is am I on the right track?
I made a template page for the list view that contains the following:
<ul>
{% for aninpatient in object_list %}
<li><a href='/inpatient-detail/{{ aninpatient.id }}/'>{{ aninpatient }}</a></li>
{% endfor %}
</ul>
Here, object_list comes from the list_detail.object_list generic view. The for loop steps through the object list object_list. In each line I create an anchor in html that references the desired href, "/inpatient-detail/nn/", where nn is the id field of each of the rows in the database table. The displayed link is the unicode string which is therefore a clickable link. I've set up templates and this works just fine.
So, am I going in the right direction? It looks like it will be straightforward to extend this to be able to put edit and delete links in the template as well.
Is there a generic view that takes advantage of the model to create the detail page? I used ModelForm helper from django.forms to make the form object, which was great for creating the input form (with automatic validation! wow that was cool!), so is there something like that for creating the detail view page?
Steve
If you're on django < 1.3 then what you are doing is basically perfect. Those generic views are quite good for quickly creating pages. If you're on django 1.3 you'll want to use the class based generic views. Once you get a handle on those they are are crazy good.
Only note I have is that you should use {% url %} tags in your templates instead of hardcoding urls. In your urls.conf file(s) define named urls like:
url('inpatient-detail/(?P<inpatient_id>\d+)/$', 'your_view', name='inpatient_detail')
and in your template (for django < 1.3):
...
In 1.3 a new url tag is available that improves life even more.