What does resolve_variable do? And could I use it for accessing the request outside of the view?
Edit
So template.Variable is the correct way to go - but I'm still unsure of its purpose. The documentation doesn't really help.
Cheers guys.
I'm assuming your trying to write a custom template tag here, so here's what you do.
In your compilation function, you bind the variable like so:
#register.tag
def my_tag(parser, token):
# This version uses a regular expression to parse tag contents.
try:
# Splitting by None == splitting by spaces.
tag_name, var_name = token.contents.split(None, 1)
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires arguments" % token.contents.split()[0]
#this will "bind" the variable in the template to the actual_var object
actual_var = template.Variable(var_name)
return MyNode(template_variable)
class MyNode(template.Node):
def __init__(self, actual_var):
self.actual_var = actual_var
def render(self, context):
actual_var_value = self.actual_var.resolve(context)
#do something with it
return result
If you only want access the request, you bind against the variable directly in the node. Make sure you have the request in the context:
from django.template import RequestContext
def my_view(request):
#request stuff
return render_to_response("mytemplate.html", {'extra context': None,}, context_instance=RequestContext(request))
Then in your template tag code.
#register.tag
def simple_request_aware_tag(parser, token):
return SimpleRequestAwareNode()
class SimpleRequestAwareNode(template.Node):
def render(self, context):
request = template.Variable('request').resolve(context)
#we want to return the current username for example
return request.user.get_full_name()
what does resolve_variable do
Resolves a variable in a template tag.
could I use it for accessing the request outside of the view
In a template tag? Yes, as long as the request is in the context - but you don't necessarily need resolve_variable for that, if you're using a simple tag or inclusion tag.
Related
In my view function I want to call another view and pass data to it :
return redirect('some-view-name', backend, form.cleaned_data)
, where backend is of registration.backends object, and form.cleaned_data is a dict of form data (but both must be either sent as *args or **kwargs to prevent raising Don't mix *args and **kwargs in call to reverse()! error). From what I've found in the docs :
def my_view(request):
...
return redirect('some-view-name', foo='bar')
It looks like I need to provide 'some-view-name' argument, but is it just the name of the view function, or the name of the url ? So I would like to make it similar to the way it's done in django-registration, where :
to, args, kwargs = backend.post_registration_redirect(request, new_user)
return redirect(to, *args, **kwargs)
def post_registration_redirect(self, request, user):
return ('registration_complete', (), {})
Ok so now, can I call directly my view function or do I need to provide a url for it ? And what more important, how my funciotn call (and a url if needed) should look like ? Both backend, and cleaned_data are just passed through this view for a later usage. I've tried this, but it's improper :
url(r'^link/$', some-view-name)
def some-view-name(request, *args):
As well as this :
return redirect('some_url', backend=backend, dataform.cleaned_data)
url(r'^link/$', some-view-name)
def some-view-name(request, backend, data):
still NoReverseMatch . But in django-registration, I've seen something like this :
url(r'^register/$',register,{'backend': 'registration.backends.default.DefaultBackend'}, name='registration_register'),
def register(request, backend, success_url=None, form_class=None,
disallowed_url='registration_disallowed',
template_name='user/login_logout_register/registration_form.html',
extra_context=None):
urls.py:
#...
url(r'element/update/(?P<pk>\d+)/$', 'element.views.element_update', name='element_update'),
views.py:
from django.shortcuts import redirect
from .models import Element
def element_info(request):
# ...
element = Element.object.get(pk=1)
return redirect('element_update', pk=element.id)
def element_update(request, pk)
# ...
Firstly, your URL definition does not accept any parameters at all. If you want parameters to be passed from the URL into the view, you need to define them in the urlconf.
Secondly, it's not at all clear what you are expecting to happen to the cleaned_data dictionary. Don't forget you can't redirect to a POST - this is a limitation of HTTP, not Django - so your cleaned_data either needs to be a URL parameter (horrible) or, slightly better, a series of GET parameters - so the URL would be in the form:
/link/mybackend/?field1=value1&field2=value2&field3=value3
and so on. In this case, field1, field2 and field3 are not included in the URLconf definition - they are available in the view via request.GET.
So your urlconf would be:
url(r'^link/(?P<backend>\w+?)/$', my_function)
and the view would look like:
def my_function(request, backend):
data = request.GET
and the reverse would be (after importing urllib):
return "%s?%s" % (redirect('my_function', args=(backend,)),
urllib.urlencode(form.cleaned_data))
Edited after comment
The whole point of using redirect and reverse, as you have been doing, is that you go to the URL - it returns an Http code that causes the browser to redirect to the new URL, and call that.
If you simply want to call the view from within your code, just do it directly - no need to use reverse at all.
That said, if all you want to do is store the data, then just put it in the session:
request.session['temp_data'] = form.cleaned_data
I do like this in django3
redirect_url = reverse('my_function', args=(backend,))
parameters = urlencode(form.cleaned_data)
return redirect(f'{redirect_url}?{parameters}')
I am new to Django. One of my project, I used render instead of redirect to send data. That worked good. My code was like this --->
for key, value in request.POST.lists():
print(key, value)
if key.split('-')[-1] != 'csrfmiddlewaretoken':
qu_id = key.split('-')[-1]
get_answer = Answer.objects.filter(question_id=qu_id,
correct_answer__option__contains=value[0])
total_correct_answer = get_answer.count()
context = {'score': total_correct_answer}
return render(request, 'result.html', context)
context = {'questions': questions, 'total_correct_answer': total_correct_answer}
return render(request, 'test.html', context)
I have an app with class based views. Each view inherits from the base view defined earlier.
Said view (and all of its descendants) implements get_context_data method which can throw exceptions under some conditions.
I would like to catch these exceptions and render specific template.
Basically views.py looks like this
class BaseView(TemplateView):
def get_context_data(**ctx):
context = super(BaseView, self).get_context_data(**ctx)
if condition:
raise CustomException('Condition was met')
# code that adds values into context dictionary
# like context['somevar'] = somevalue
...
return context
class SpecificView1(BaseView):
def get_context_data(**ctx):
context = super(SpecificView1, self).get_context_data(**ctx)
# code that updates values in context dictionary
...
return context
I've tried to insert a new class into views hierarchy (renaming BaseView into OriginalBaseView)
class BaseView(OriginalBaseView):
def get_context_data(**ctx):
context = {}
try:
context = super(BaseView, self).get_context_data(**ctx)
except RepoException:
self.template_name = 'specific_template.html'
return context
But using this approach would mean that I will have to update all descendant get_context_data methods with checks that keys they update are in context.
Is there a way to better handle this?
You can write your own middleware which will handle RepoException and render specific_template.html
https://docs.djangoproject.com/en/3.0/topics/http/middleware/#exception-middleware
This way you can throw RepoException anywhere in view and your template will be rendered automatically.
batiskaf answer seems to be right. You basically need to put a middleware in place which processes your view exceptions. This middleware can either send None in which case the exception gets handled, or a HTTP Response , which can have your template.html rendered.
def process_exception(exception, *args, **kwargs):
error_code = getattr(exception, 'code', 500) #you can use codes if you want
if not isinstance(exception, MyBaseException):
request = kwargs.get('request') #you can use request to load the template
return {
'status': 0,
'message': exception.message,
'error_code': error_code
} #or send an HttpResponse with your template.
Hope this helps :)
Here is a url containing the hash for a super-secret feed:
http://127.0.0.1:8000/something/feed/12e8e59187c328fbe5c48452babf769c/
I am trying to capture and send the variable '12e8e59187c328fbe5c48452babf769c' which is feed_hash (acts as a slug to retrieve the particular entry)
Based on the example in django-syndication, I've created this simple class in feeds.py
class SomeFeed(Feed):
title = 'feed title '+request.feed_hash #just testing
link = "/feed/"
description = "Feed description"
def items(self):
return Item.objects.order_by('-published')[:5]
def item_title(self, item):
return item.title
def item_description(self, item):
return item.content
# item_link is only needed if NewsItem has no get_absolute_url method.
def item_link(self, item):
return 'link'
Hence I am wondering, how would I modify this to get a model according to the hash?
At this time I cannot access the 12e8e59187c328fbe5c48452babf769c in any way. How might I access this and -- in a standard Django way -- create a feed from the retrieved variable (which represents a slug accessing a many-to-many relationship.)
First of all, set your parameter in django URL dispatcher. Something like:
url(r'^feed/(?P<pid>\w+)/$', SomeFeed())
Now retrieve and return the hash from the URL using the get_object method on your feed class. After all, get the hash as the second parameter of your method items().
class SomeFeed(Feed):
def get_object(self, request, pid):
# expect pid as your second parameter on method items()
return pid
# you can also load an instance here and get it the same way on items()
return SomeFeed.objects.get(pk=pid)
def items(self, feed):
# filter your feed here based on the pid or whatever you need..
return Item.objects.filter(feed=feed).order_by('-published')[:5]
I have a django template with a context variable myVar, set in the view function.
This template also renders a custom simple template tag {% myTemplateTag %} that renders myTemplate.html
I want to use myVar inside the custom template tag that renders myTemplate.html.
Is there a way to inherit the context variables of my view function in the custom template tag? (without passing it explicitly as a parameter to the template tag)?
Using simple_tag
Using simple_tag, just set takes_context=True:
#register.simple_tag(takes_context=True)
def current_time(context, format_string):
timezone = context['timezone']
return your_get_current_time_method(timezone, format_string)
Using a custom template tag
Just use template.Variable.resolve(), ie.
foo = template.Variable('some_var').resolve(context)
See passing variables to the templatetag:
To use the Variable class, simply instantiate it with the name of the
variable to be resolved, and then call variable.resolve(context). So,
for example:
class FormatTimeNode(template.Node):
def __init__(self, date_to_be_formatted, format_string):
self.date_to_be_formatted = template.Variable(date_to_be_formatted)
self.format_string = format_string
def render(self, context):
try:
actual_date = self.date_to_be_formatted.resolve(context)
return actual_date.strftime(self.format_string)
except template.VariableDoesNotExist:
return ''
Variable resolution will throw a VariableDoesNotExist exception if it cannot resolve the string passed
to it in the current context of the page.
Might be useful too: setting a variable in the context.
Perhaps you could include the myTemplate.html file instead of rendering it with a special tag? Have you looked at the include tag? If you include myTemplate.html it will share the context with the one including it.
In my view function I want to call another view and pass data to it :
return redirect('some-view-name', backend, form.cleaned_data)
, where backend is of registration.backends object, and form.cleaned_data is a dict of form data (but both must be either sent as *args or **kwargs to prevent raising Don't mix *args and **kwargs in call to reverse()! error). From what I've found in the docs :
def my_view(request):
...
return redirect('some-view-name', foo='bar')
It looks like I need to provide 'some-view-name' argument, but is it just the name of the view function, or the name of the url ? So I would like to make it similar to the way it's done in django-registration, where :
to, args, kwargs = backend.post_registration_redirect(request, new_user)
return redirect(to, *args, **kwargs)
def post_registration_redirect(self, request, user):
return ('registration_complete', (), {})
Ok so now, can I call directly my view function or do I need to provide a url for it ? And what more important, how my funciotn call (and a url if needed) should look like ? Both backend, and cleaned_data are just passed through this view for a later usage. I've tried this, but it's improper :
url(r'^link/$', some-view-name)
def some-view-name(request, *args):
As well as this :
return redirect('some_url', backend=backend, dataform.cleaned_data)
url(r'^link/$', some-view-name)
def some-view-name(request, backend, data):
still NoReverseMatch . But in django-registration, I've seen something like this :
url(r'^register/$',register,{'backend': 'registration.backends.default.DefaultBackend'}, name='registration_register'),
def register(request, backend, success_url=None, form_class=None,
disallowed_url='registration_disallowed',
template_name='user/login_logout_register/registration_form.html',
extra_context=None):
urls.py:
#...
url(r'element/update/(?P<pk>\d+)/$', 'element.views.element_update', name='element_update'),
views.py:
from django.shortcuts import redirect
from .models import Element
def element_info(request):
# ...
element = Element.object.get(pk=1)
return redirect('element_update', pk=element.id)
def element_update(request, pk)
# ...
Firstly, your URL definition does not accept any parameters at all. If you want parameters to be passed from the URL into the view, you need to define them in the urlconf.
Secondly, it's not at all clear what you are expecting to happen to the cleaned_data dictionary. Don't forget you can't redirect to a POST - this is a limitation of HTTP, not Django - so your cleaned_data either needs to be a URL parameter (horrible) or, slightly better, a series of GET parameters - so the URL would be in the form:
/link/mybackend/?field1=value1&field2=value2&field3=value3
and so on. In this case, field1, field2 and field3 are not included in the URLconf definition - they are available in the view via request.GET.
So your urlconf would be:
url(r'^link/(?P<backend>\w+?)/$', my_function)
and the view would look like:
def my_function(request, backend):
data = request.GET
and the reverse would be (after importing urllib):
return "%s?%s" % (redirect('my_function', args=(backend,)),
urllib.urlencode(form.cleaned_data))
Edited after comment
The whole point of using redirect and reverse, as you have been doing, is that you go to the URL - it returns an Http code that causes the browser to redirect to the new URL, and call that.
If you simply want to call the view from within your code, just do it directly - no need to use reverse at all.
That said, if all you want to do is store the data, then just put it in the session:
request.session['temp_data'] = form.cleaned_data
I do like this in django3
redirect_url = reverse('my_function', args=(backend,))
parameters = urlencode(form.cleaned_data)
return redirect(f'{redirect_url}?{parameters}')
I am new to Django. One of my project, I used render instead of redirect to send data. That worked good. My code was like this --->
for key, value in request.POST.lists():
print(key, value)
if key.split('-')[-1] != 'csrfmiddlewaretoken':
qu_id = key.split('-')[-1]
get_answer = Answer.objects.filter(question_id=qu_id,
correct_answer__option__contains=value[0])
total_correct_answer = get_answer.count()
context = {'score': total_correct_answer}
return render(request, 'result.html', context)
context = {'questions': questions, 'total_correct_answer': total_correct_answer}
return render(request, 'test.html', context)