Hi Stackoverflow people,
I am using the Userena package for my user registration site. The package allows to change the template or the form class during with the call of the views.py function "def profile_edit" (if I understood it correctly).
The full header of the view function is:
def profile_edit(request, username, edit_profile_form=EditUserProfileForm,
template_name='userena/profile_form.html', success_url=None,
extra_context=None):
The demo project calls the view function in the template through the urls.py with the statement
{% url userena_profile_edit user.username %}
When I try to change the form parameter for example with
{% url userena_profile_edit user.username edit_profile_form=EditUserProfileForm %}
I get the follow error, which does not make sense to me:
Caught ValueError while rendering: Don't mix *args and **kwargs in call to reverse()!
I have also tried to the specify the kwargs dict, but it did not work either.
{% url userena_profile_edit user.username kwargs={'edit_profile_form':EditUserProfileForm} %}
How can I correctly call the function? I am confused why the last statement would not work.
Thank you for your help!
That's because your mixing args and kwargs. You can't do that in a reverse call. user.username is an arg, try using it as a kwarg:
{% url userena_profile_edit username=user.username edit_profile_form=EditUserProfileForm %}
how about making the username a kwarg?
{% url userena_profile_edit username=user.username edit_profile_form=EditUserProfileForm %}
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
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 have a URL defined like this in Django:
# http://localhost:8000/quiz/grammar/beginner/1/question
url(r'^(?P<page_name>[-\w]+)/(?P<level>\w+)/(?P<quiz_id>\d+)/question/$', views.question, name='question')
If I pass the static values I don't get any errors:
{{ i.name }}
Since I am already on this page: http://localhost:8000/quiz/grammar/beginner/ I thought of passing URL like this:
{% for i in quizes %}
{{ i.name }}
{% endfor %}
I have namespace defined. But I get this error:
Reverse for 'question' with arguments '(1L,)' and keyword arguments '{}' not found.
I am doing like this in my view:
def question(request, quiz_id):
What's wrong?
EDIT: Tried this, still no luck:
def question(request, page_name, level, quiz_id):
The url tag returns an absolute url. It is not aware of the page your are on, so you cannot leave out the the level and quiz arguments when using the tag.
If you have page_name and level in your template context, you can use these variables in the url tag.
{{ i.name }}
If you capture page_name and level arguments in your url, then you are correct to change your view function to accept them.
def question(request, page_name, level, quiz_id):
I have project in Django 1.3. In order to show username in all pages I use such tags in base.html
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}.
loggout</p>
{% else %}
loggin
{% endif %}
But if I dont return context_instance=RequestContext(request) from view value of user in template is empty. The 'django.contrib.auth.context_processors.auth' is included to TEMPLATE_CONTEXT_PROCESSORS.
Is it possible automaticaly include user to all templates?
since django 1.3. use shortcuts.render function and dont warry about requestcontext including to your views
You've given the answer yourself. As long as you use a RequestContext, it will be included in all templates.
If you really find that too much work, you could use the (new in 1.3) TemplateResponse class.
Or simply create a context processor. See
http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
Put this in context_processor.py
def root_categories(request):
return {
'user': request.user,
}
in settings.py add the context processor.
now in your template try: {{ user }}
I`m new with django (came from Grails), especially with all those custom tags that you have to deal with, instead of writing your variables directly inside the templates.
Well, what I need to do was something really simple, but for some reason is taking me a long time to finish. What I wish to do was make a tag that checks for me if the given path is equals my current url, and then returns the class if true.
<li class="{% check_url '/login/' 'current_page_item' %}">
login
</li>
But, the problem came when I tried to register the tag with takes_context :
Caught TypeError while rendering: simple_tag() got an unexpected keyword argument 'takes_context'
from django import template
register = template.Library()
#register.simple_tag(takes_context=True)
def check_url(context, path, attr):
if context['request'].environ.get('PATH_INFO') == path:
return attr
else:
return ''
How can I fix it? Also, is there a better way to do it?
That's because takes_context is only available since django 1.3.
Another approach to do it (and to avoid hardcoded urls):
{% url social_login as the_url %}
{% ifequal the_url request.path %}
....
{% endif %}
Or check out something like this!