Append information at the end of the URL - django

I have an application which is very similar to forum. Users can participate in posting content. When user click on a topic it goes to that topics page which shows all the discussion related to that topic. I have a side bar just like in the stackoverflow where it shows similar questions which shows topics related to the title of the topic in the current page.
Here is the sidebar template code:
<div class="box">
<h2>{% trans %}Related Topics{% endtrans %}</h2>
<div class="topic-related">
{% for thread_dict in similar_threads.data() %}
<p>
{{ thread_dict.title|escape }}
</p>
{% endfor %}
</div>
</div>
I have an application that tracks user clicks. Assume that user went to a topic and after seeing the related topics she clicks on a topics and go to that page. But I have no way of distinguishing if the user directly went to this topics other than using related topics section.
So I thought may be I can add something like fromRelatedTOpics to the end of the url. What is the best way to accomplish this?
{{ thread_dict.title|escape }}
Is this possible?

The keyword you are looking for is referer. If the user clicked a link to your site, the referer may tell you where he came from (this depends on the browser setting). To access the referer from a view, you have to access the META attribute of the request, i.e.:
request.META.get('HTTP_REFERER')
You may want to look into this django snippet to get some inspiration.
If you want this information inside the template, you can try this:
{{ request.META.HTTP_REFERER }}

What you can do is to redirect all clicks to "related topics" to a single view, also pass the related topic id (or any unique value which can identify that topic in backend) to this view.
Now whenever this view is executed you can safely assume that someone clicked on "related topic", so record this behavior. Using referer (as described by #steinar) you can also record the parent page url from where relative link was clicked.
After recording the behavior you can redirect user to "related topic" using the unique id passed to this view.

Related

Is it possible to display the HTML provided by the admin panel app on-site?

I've built a site where I can create new posts (essays) by the admin panel. The output is visible to users. But when I place some HTML as content in the form it doesn't render itself on the page.
example:
Output on the page (with marked unrendered HTML):
I would like to know how to fix it and also, how to name the topic I want to know ( I couldn't find anything related to my problem, probably because I don't know how to express it).
Additionally, I just start to wonder if there is one more problem nested inside. How to link CSS from the static folder having this HTML mentioned above?
Django offer the autoescape template in the builtins tags
{% autoescape off %}
{{ myhtml }}
{% endautoescape %}
But your logic seems wrong, you don't need to create a new page with the doctype, just create a base template and use the block content tag to insert your article.
In your base template replace the description and title of your page by variables that will be populated by the article data.
You need to learn the basic of Django https://docs.djangoproject.com/en/4.1/ trust me you won't regret it !

django redirect after post with post parameters after update

I was looking for help but couldn't find any suitable solution.
I would like to ask about redirect after POST.
Situation looks as below step by step:
Simple search form with POST with parameter called 'find' within form. Search form appears on every site and if i call search on site with devices i'm checking referrer and i'm doing query for device model fields and results go to devices.html template. Same situation is when I search within localization site, checking for referrer and querying for model fields of localization model and returning query result in localization.html template.
When I try to update device i'm opening new edit device template with url '/edit/device/dev_id' when every field of this model is shown so i can update some fields manually.
After commiting changes on edit site device 'post' goes to url 'update/device/dev_id' and changes to device are saved properly.
The problem is how can I redirect after updating device to step number one where are the results of search view for devices?
If i redirect after update ('update/device/dev_id') device to 'request.META.get('HTTP_REFERER')' i'm getting 'edit/device/dev_id' url ?
Worth to mention is that method POST for search form sends text search input to action="/search" and then after checking referrer shows results, so there is nothing like 'action="search/find".
I know that in web browser we can do few times previous click to show search results incuding POST parameters but it is not the point.
If you have any ideas how to redirect to search results (method POST, 1 point) after updating please let me know.
Regards AD
I will try to simplify the question ( too much code to paste ):
Given search below in template:
<form class="navbar-form navbar-left" action="/search" method="post">{% csrf_token %}
<div class="form-group">
<input type="text" class="form-control" name="find" placeholder="Search">
</div>
<button type="submit" class="btn btn-default" >Search</button>
</form>
in views.py:
def search_dev(request):
to_find=request.POST.get('find')
(..)
<--query updating_fields in instance considering only 1 search result-->
redirect ? (to search query with POST )
Is it possible to go back to search results after some changes to instance ( for example update ) considering only 1 search result ?

Need user to pick from a list to connect two users in YESOD

I need general guidance on how to structure a YESOD app. I would like to keep the app as "RESTful" in design as possible.
The user searches all the other users to find one to connect with. I show the possible connections using Hamlet:
$forall (pid, person, mEmail, mPhone) <- displayPeopleRecs
<p>
<a href=#{CreateFundR pid}>#{personNickName person}
$maybe email <- mEmail
#{emailEmail email}
$maybe phone <- mPhone
#{phoneNumber phone}
However, now when a user clicks on a link they go to the /createfund/ page as a GET request which is not what I want, I want to use POST or something else.
Can anyone explain what the correct solution is here? Do I make a form for each person what the search produces and have a submit button for each possible person? That seems silly. Is it better to use Julius and change the onclick handler for the link to submit a POST instead of a GET to /createfund ?
Here is the relevant line from my config/routes:
/createfund/#PersonId CreateFundR POST
By the way, I can see how to make this work by using a form and a submit button:
$forall (pid, person, mEmail, mPhone) <- displayPeopleRecs
<p>
<form method="post" action="#{CreateFundR pid}">
<table>
<tr>
<td>
#{personNickName person}
$maybe email <- mEmail
<br>
#{emailEmail email}
$maybe phone <- mPhone
<br>
#{phoneNumber phone}
<td>
<input type="submit" value="Create Fund">
That will work for my needs, but I'd really like to allow the user to just click the link. Is this poor design? Or just a matter of taste?
If you use an AForm / MForm, the form will be automatically generated for you (using Tables or Divs). That should simplify things for you.
If you want to manually style it, you can do something like this when using a form: How to make button look like a link?. Most people end up creating styled buttons for such actions anyways (think of your standard CRUD app with Edit, Delete buttons, etc.).
If you go down the path of trapping link clicks and do ajax Post, it will not degrade nicely if javascript is disabled so something you need to watch out for.
HTH

django-cms redirect top level menu to first child

I would like to redirect the top menu items in a page I'm writing to point at their children if they have any. E.g. this site has a top level About, with several CMS pages underneath. When a user clicks on about, I want to show the first child page under the top-level about menu.
I can do it like the below, but it feels wrong. I know I can do a static redirect, but I don't want to do that, as the first child might change (it's a CMS system after all) and I don't want the people entering data into the CMS to have to manage a redirect. So I prefer the below to a configured redirect.
This is a pretty common requirement, is there a standard way of doing this? I think I might be able to do it with a navigation modifier, so I might try that route next.
<div style="display:none;">{% show_menu 0 1 1 %}</div>
<div id="topmenu">
<ul>
{% for child in children %}
<li>
{% if child.children %}
<a href="{{child.children.0.get_absolute_url}}">
{% else %}
<a href="{{child.get_absolute_url}}">
{% endif %}
{{ child.get_menu_title }}
</a>
</li>
{% endfor %}
</ul>
</div>
About the above, I need to have the show_menu in there otherwise the data isn't there for my custom menu. Is there any way for this not to be needed?
The thing I dislike the most about the above, is that the /about/ page is still active. It's not the worst thing in the world if someone browses there, they'll get an empty page, but I'd rather that not happen. I think if I wrote a navigation extension the same disadvantage would be there. I guess I need a programmatic redirect to redirect to the first child if that page is hit. Thoughts? I can probably do it in template logic, but that seems crazy.
2018: updated version, that works in cms 3.4+ and new middleware style compatible available here. Once you added the /firstchild, you'll need to edit/publish the redirecting page in the cms pages's change list, as, you guess it, will redirect.
solution with middleware and custom redirect field value of "/firstchild", to trigger redirects.
plus: you won't have any blank pages
plus: it's flexible, as it allows pages that will not be redirected to subpages
plus: no action needed when the first child's url changes
minor drawback: there is always a redirect ( 301, though, not that bad ;)
minor drawback: configuration is a bit nerdy, as you will have to enter "/firstchild" into the "redirect" field. definitely not end-user friendly, but works. the cms itself doesnt provide a better solution at all (not even in 2018).
from django.shortcuts import redirect
# redirect to first subpage if the "redirect" field is exactly "_subpage"
class RedirectSubpageMiddleware(object):
def process_view(self, request, view_func, view_args, view_kwargs):
if request.current_page:
page = request.current_page
if "slug" in view_kwargs and page.get_redirect() == "/firstchild":
subpages = request.current_page.children.all()
if subpages.count():
# can redirect! must make uri!
return redirect(subpages[0].get_absolute_url(), permanent=True)
return None
You might consider setting the CMS_REDIRECTS = True in the settings.py file.
Then you can go into the /about/ page in the admin and add the redirect to /about/child-page
Note: I haven't actually tried this, I just happened to stumble across this information this morning.

Create a PDF or Printable version of change_list.html in Django Admin

I would like to add a tool link at the top of my admin change_list.html, which I have already done, and have this link basically be able to produce some sort of printable document version of my models data based off of my current filter settings. Basically a print button in the admin change_list.html.
so far I have overridden the change_list.html to create the link, and I notice that this
<li>
<a href="{{ choice.query_string|iriencode }}" class="addlink">
{% blocktrans %}View PDF{% endblocktrans %}
</a>
</li>
gives you a link based on these choices.. but Im kinda lost as to the best/easiest way to do this..
Sorry, new to Django. I know I can use ReportLabs to generate pdfs, but not a 100% on how to get the filtered data from change_list to it.
A bit late, but for those who might be searching "in the future" like me, this might be helpful: http://djangosnippets.org/snippets/1842/