Using Django, I am sending an html email that contains links but I can't get the links to work. I've tried using {{request.get_full_path}} in my template, as well as request.build_absolute_uri() in my view. Any help would be great!
In my site, the correct url is given in the template like this, with url="/review_and_export/":
BibTex
Currently, this is my email code:
views.py
# for links in html email??
full_url = request.build_absolute_uri()
full_url = full_url.split("/software/", 1)[0] #this produces just the domain, EDIT: i was missing a slash...still having issues
html_content = render_to_string(email_it, {'full_url' : full_url,....}, context_instance=RequestContext(request))
email_it.html
{% for publication in value %}
{% include "software/display_citations.html" with publication=publication url=full_url%}
{% endfor %}
display_citations.html
# I use full_url and request.get_full_path because get_full_path doesn't return the domain
BibTex
I figured it out by further manipulating the url in my views and then concatenating that result with the argument in my template. Thank you for your help!
Related
In Views.py
return HttpResponseRedirect('add_scenes?submitted=True?linkToPrevScene=%s'%ScenePrevious)
ScenePrevious is a string containing "water2"
In my URL of add_scenes.html it works :
http://127.0.0.1:8000/scenes3d/add_scenes?submitted=True?linkToPrevScene=water2
But in the file add_scenes.html
{% if submitted %}
your scene was submitted successfully after {{ linkToPrevScene }}
{% else %}
this HTML code doesn't give output for {{ linkToPrevScene }} although {% if submitted %} is evaluated correctly
edit of my post:
This line also doesn't work if I replace the second ? by &
return HttpResponseRedirect('add_scenes?submitted=True&linkToPrevScene=%s'%ScenePrevious)
Sorry I couldn't comment but you need to pass the parameter linkToPrevScene through the view for you to receive it in the template.
**Note:**In Django You can not pass parameters with redirect. Your only bet is to pass them as a part of URL.
#...Rest of your view...
context['linkToPrevScene'] = Water2
redirect(reverse('add_scenes?submitted=True?linkToPrevScene=%s'%ScenePrevious, kwargs={ 'linkToPrevScene': Water2 }))
Use the links below for reference
Django documentaion:
Reverse
Redirect
my code is simple but I admit I have taken it from the internet.
I want to update a model with a button click. Which model, it is chosen by the variable. So I have this html code:
<form action="{% url 'reception:update_status' slug=name %}" method="POST">
{% csrf_token %}
<input type="submit" value="has arrived">
</form>
This code is in url.py
re_path('update_status/(?P<slug>[-a-zA-Z0-9_]+)$', views.update_status, name='update_status'),
and in views.py
def update_status(request, slug):
if request.method == 'POST':
p = MyModel.objects.filter(name=slug)
p.status = 'is waiting'
p.update()
return redirect(request, 'home')
Now with the code like this it comes back with the following error when I click on the button!
Reverse for '<WSGIRequest: POST '/update_status/Name10'>' not found. '<WSGIRequest: POST '/update_status/Name10'>' is not a valid view function or pattern name.
and I have no clue what this means. Please help?
Thanks
For regex patterns in Django 2.0, you need to use re_path, instead of path for your url.
I solved this problem with pk rather than slugs.
I would recommend everyone who is new to Django to create an id field for every Model. Because that can then be used to pass parameters very easy. Slug is more complicated and makes life unnecessarily difficult!
After a new article is posted (in a form via Ajax) on my Django news site, I want to return a link to the article.
To do so, I'm using a Template object that says:
if form.is_valid():
form.instance.user = request.user
new_article = form.save()
success = Template('<div id="panel_input" class="col-lg-4"> <h2 class="text-center"> Success </h2> <p class="text-center"> Your Article has been posted. You can see and edit details by clicking here. </p></div>')
context = Context({"article_id" : new_article.pk})
return HttpResponse(success.render(context))
The urlsConf for this looks like:
...
url(r'^article/(?P<article_id>\d+)/$', views.article, name='article'),
...
The problem is that I get an error because of {% url "article_manager:article" %}/{{ article_id }}. Apparently, I must pass the article_id inside the previous tag, since the urlsConf requires the id parameter.
But I also get an error when I put the second tag inside the first, like this:
{% url "article_manager:article" {{ article_id }} %}
I'm not sure how to accomplish this task, it doesn't seem to work with the tools I have. Does anyone have any suggestions?
Try {% url "article_manager:article" article_id=article_id %}
Maybe a little more explanation is needed: You were calling the template tag right {% url "namespace:name" %}. Remember that some templatetags can take arguments, in the *args, **kwargs form. The args can be any simple expression understood by the template language, including a context variable (no need to add double-braces). The kwargs follow the same rule, and have the form argument=expression. Thus, you can call some template tags with the form {% tag "exp" 1 request number=5 username=user.name %}
I defined my custom Django filter youtube_embed_url in templatetags/custom_filters.py. It takes an Youtube url and returns the string which is embed code for the video. The code for templatetags/custom_filters.py is below:
from django import template
from django.conf import settings
register = template.Library()
import re
#register.filter(name='youtube_embed_url')
# converts youtube URL into embed HTML
# value is url
def youtube_embed_url(value):
match = re.search(r'^(http|https)\:\/\/www\.youtube\.com\/watch\?v\=(\w*)(\&(.*))?$', value)
if match:
embed_url = 'http://www.youtube.com/embed/%s' %(match.group(2))
res = "<iframe width=\"560\" height=\"315\" src=\"%s\" frameborder=\"0\" allowfullscreen></iframe>" %(embed_url)
return res
return ''
youtube_embed_url.is_safe = True
Then I use this filter in link_page.html page. Here is the relevant portion of link_page.html:
<div>
{{ link.url|youtube_embed_url }}
</div>
However, when I view the link page in browser I see the HTML code as the string:
Any idea how to make the result of youtube_embed_url method to be interpreted as the HTML code, not a string? Thanks in advance, guys!
Good ol' safe filter.
{{ link.url|youtube_embed_url|safe }}
You can also use django-embed-video.
Usage is quite similar:
{% load embed_video_tags %}
{{ link.url|embed:'560x315' }}
I am having issues with get_absolute_url in my django templates when using django page cms.
It returns an empty string and does not link to my required page.
I have the following Models, URLs templates and views
Models
class Body(models.Model):
...
url = models.SlugField(unique=True, help_text='---')
urls
(r'^news/', include('news.urls_news')),......
url(r'^/(?P<url>[\w\-]+)/$', 'news_view', name='news_view'),
View
def news_view(request, url):
new = get_object_or_404(Body, url=url)
return render_to_response('news/view.html', {
'news': news
}, context_instance=RequestContext(request))
Template
<li>{{ news.title }}</li>
the following code in my template returns the string I desire however this does to direct to my html page
<li>{{ news.title }}</li>
I know my everything links to the correct files because I have other views that work correctly. Could somebody please point me in the correct direction as to why get_absolute_url is not working correctly and why {{ news.url }} does not direct to the correct page. I am sure it has something to do with my urls.py however I am not certain.
Please bear with me I am new to django. All help is greatly appreciated.
Have you actually defined a get_absolute_url method on the News model? You don't show it.