English 中文(简体)
更改/更新视图后 Django 错误
原标题:Django Error After Changing/Reverting Views

先前我正在改变一些有关地理定位(失灵)的观点,

TemplateSyntaxError at /report/all/
Caught NoReverseMatch while rendering: Reverse for  profiles_profile_detail  with arguments  (  ,)  and keyword arguments  {}  not found.

奇怪的是,我修改并恢复的视图与此视图或模板无关。 urls. py 文件根本没有被触动。 应用程序中所有其他页面都正常显示。 我无法找到问题所在 。

意见:

from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext
from django.core.urlresolvers import reverse
from myapp.report.models import Story, UserProfile
from myapp.report.forms import ProfileForm, StoryForm
from django.contrib.auth.decorators import login_required
from django.contrib.gis.utils import GeoIP

def all_stories(request):
    if not request.user.is_authenticated():
        return redirect("django.contrib.auth.views.login")
    all_stories = Story.objects.all().order_by("-date")

    return render_to_response("report/storyline.html",
                                { stories : all_stories},
                                context_instance=RequestContext(request))

def story_detail(request, story_id):
    story = get_object_or_404(Story, id=story_id)
    return render_to_response( report/detail.html ,
                            { story : story},
                                context_instance=RequestContext(request))

@login_required
def submit_story(request):
    if request.method =="POST":
        story_form = StoryForm(request.POST, request.FILES)
        if story_form.is_valid():
            new_story = story_form.save(commit=False)
            new_story.author = request.user
            new_story.save()
            return HttpResponseRedirect("/report/all/")
    else: # GET request
        story_form = StoryForm()
    return render_to_response("report/report.html", { form : story_form}, context_instance=RequestContext(request))

表格( 已修改但已恢复; 似乎正在工作) :

from django import forms
from stentorian.report.models import UserProfile, Story
from django.contrib.gis.utils import GeoIP

class ProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile

class StoryForm(forms.ModelForm):
    class Meta:
        model = Story
        exclude = ( author ,)

模板 :

{% extends  base.html  %}

{% block page_title %}Stentorian{% endblock %}
{% block headline %}Stentorian Storyline{% endblock %}

{% block content %}

    <div class="row">
    <div class="span12">

        <h2>Welcome <a href="{% url profiles_profile_detail user.username %}">{{ user.username }}</a></h2>


 <div class="accordion" id="story_accordion">
        {% for story in stories %}
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a class="accordion-toggle story-header" data-toggle="collapse" data-parent="#story_accordion" href="#story_{{ story.id }}">
                         {{ story.title }} - {{ story.author.username }} - {{ story.date }}
                    </a>
                </div>
                <div id="story_{{ story.id }}" class="accordion-body collapse{% if forloop.counter0 == 0 %} in{% endif %}">
                    <div class="accordion-inner">
                        <!-- <h2><a href="{% url detail story.id %}">{{story.title}}</a></h2>-->
                        <span><a href="{% url profiles_profile_detail story.author %}">{{story.author}}</a> </span><br>

                        <span>{{story.topic}}</span><br>
                        <span>{{story.zip_code}}</span><br>

                        <span>{{story.date}}</span><br>

                        <p>{{story.copy}}</p>
                    </div>
                </div>
            </div>

            <br>

        {% endfor %}
        </div>

    </div>
</div>

{% endblock content %}

错误即将出现在线上 :

{% for story in stories %}

如果有人能提供为什么发生这种情况的答案,那将非常感激。 同样,url没有改变,这似乎是这一错误的主要原因。

问题回答

似乎没有获得用户。 username

试试这个

 <h2>Welcome <a href="{% url profiles_profile_detail request.user.username %}">{{ request.user.username }}</a></h2>




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签