django 1.5:无法获取视图中的变量值

时间:2013-12-30 13:43:43

标签: python django django-urls

我是django的新手。我有django 1.5。我想从一个模板到另一个模板获取两个变量。但我没有收到价值而只是没有收到任何价值。

我想要将变量作为url的一部分发送到的模板的代码是:

<div class="tags">
                  {% for tags1 in document.tags.all %}
                    {% if tags1.name|length > 0 %}
                    {% with tags1.name as tagts %}{% endwith %}
                    <a href="{% url 'tag_related_document' user tagts %}">{{ tags1.name }}</a>
                    {% endif %}
                  {% endfor %}
                  </div>

我调用Second View的URL代码是:

url(r'^tag_related/(?P<username>[-\w\.@]+)/(?P<tagts>)/$',
       tag_related_document,
       {'template_name': 'document/tag_related_document.html'},
       name = 'tag_related_document'
    ),

视图代码tag_related_document是:

def tag_related_document(request, username, tagts, template_name , page_template="userprofile/entry_index_page.html"):
    """
   documents  Related to tag    """
    try:
        notifications = request.user.notifications.all()[:100]
    except AttributeError:
        notifications = ""

    user = get_object_or_404(User, username = username)

    try:
        user_profile = user.get_profile()
    except UserProfile.DoesNotExist:
        messages.error(request, "Profile does not exist.")
        return HttpResponseRedirect(reverse('homepage'))

    documents = Document.objects.filter( Q(tags__in = user_profile.tags.all()) , date_deleted = None).order_by("-date_added")
    if not documents:
        documents = Document.objects.filter( date_deleted = None).order_by("?")
    related_doc_widget = Document.objects.filter( Q(tags__in = user_profile.tags.all()) , date_deleted = None).order_by('?')
    if not related_doc_widget:
        related_doc_widget = Document.objects.filter( date_deleted = None).order_by("?")

    followers_widget = Felloz.objects.get_followers(user = user_profile.user)
    followings_widget = Felloz.objects.get_followings(user = user_profile.user)
#    recommendations_widget = UserProfile.objects.filter(Q(~Q(user__in = followings_widget) or ~Q(user = request.user) ) ).order_by('?')

    recommendations_widget = UserProfile.objects.filter( tags__in = user_profile.tags.all() ).distinct().exclude(user__in = followings_widget).exclude( user = user )
    if not recommendations_widget:
        recommendations_widget = UserProfile.objects.filter(~Q( user__in = followings_widget ) ).exclude( user = user ).order_by('?')

    ideas_widget = Idea.objects.filter(user = user).order_by('?')
    for idea in ideas_widget:
            is_substring = "<a" in idea.idea_html
            if(is_substring is False):
                idea.idea_html = truncate_chars(idea.idea_html,50)
            else:
                idea.idea_html = htmltruncate.truncate(idea.idea_html,50,ellipsis='')   

    no_of_documents = len( Document.objects.filter(author = user) )
    no_of_ideas = len(Idea.objects.filter(user=user))
    try:
        slug_status = SlugStatus.objects.get(user=user)
    except SlugStatus.DoesNotExist:
        slug_status = ''

    context = {
        'slug_status':slug_status,
        'documents': documents, 
        'page_template': page_template,
        'followers_widget': followers_widget,
        'followings_widget': followings_widget,
        'related_doc_widget': related_doc_widget,
        'user': user,
        'site': Site.objects.get_current(),
        'profile':user_profile,
        'recommendations_widget': recommendations_widget,
        'no_of_documents':no_of_documents,
        'no_of_ideas': no_of_ideas,
        'notifications': notifications,
        'notifications_unread': request.user.notifications.unread(),
        'idea_widget': ideas_widget,
        'tagts':tagts,
    }
    print '=============i m here==================='
    print tagts
    print '=============i m here==================='
#related_doc_widget = Document.objects.filter(~Q(author = user) and ~Q(tags__in = UserProfile.tags.all())).order_by('?')
#recommendations_widget = UserProfile.objects.filter(~Q(user__in = followings_widget) and ~Q(user = user)).order_by('?')

#documents_newsfeed = Document.objects.filter(Q(author = user) | Q(author__in = followings_widget), date_deleted = None).order_by("-date_added")

    if request.is_ajax():
        template_name = page_template
    return render_to_response(template_name, context, context_instance=RequestContext(request))

    return render_to_response(template_name, context, context_instance=RequestContext(request))

我希望显示该变量的第二个模板的代码是:

{% if profile.user == request.user %}
            <h2 class="mainHeading"> Documents Related To: {{tagts}} </h2>
        {% else %}
            <h2 class="mainHeading"> Documents Related To: {{tagts}}</h2>
            {% endif %} 

但我什么都没有来......请提前帮助谢谢....

2 个答案:

答案 0 :(得分:0)

您在{% with %}之前关闭{% url %},因此网址基于空值,我认为这就是原因。

请在将来避免发布大量不相关的代码。将其缩小到重现错误所需的最小量。

答案 1 :(得分:0)

除非代码与您发布的代码不相似,否则应该是这样的。

{% for tags1 in document.tags.all %}
      {% if tags1.name|length > 0 %}
        {% with tags1.name as tagts %}
         <a href="{% url 'tag_related_document' user tagts %}">{{ tags1.name }}</a>
        {% endwith %}
      {% endif %}
 {% endfor %}