AttributeError'str'对象没有属性'path'

时间:2014-02-07 06:22:41

标签: django django-class-based-views attributeerror

我已为此项目实现了内联formset,每当我转到此视图时,它都会显示标题中指出的错误。在此之前,我的队友使用bootstrap.js在页面上实现了选项卡式导航。为了使标签工作,它正在寻找每个视图的请求。我之前已经解决了同样的问题,但修复了它,因为它不是基于类的视图。

现在,它显示了相同的错误,但是从不同的视图来看,这次是来自CreateView,它并没有真正返回bootstrap正在寻找的请求。我不确定如何解决这个问题,所以有人可以建议我开始排除故障吗?

顺便说一下,我不是那种经验丰富的Django,还在学习,但足以了解角落和缝隙。

部分代码:

views.py (摘录,借用了Kevin Dias的一些代码)

class AddRecipe(CreateView):
    template_name = 'addrecipe.html'
    model = Recipes
    form_class = RecipeForm
    success_url = '/success/'

    def get(self, request, *args, **kwargs):
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        ingredient_form = IngredientFormSet()
        instruction_form = InstructionFormSet()
        return self.render_to_response(
            self.get_context_data(form = form,
                                  ingredient_form = ingredient_form,
                                  instruction_form = instruction_form),)

    def post(self, request, *args, **kwargs):
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        ingredient_form = IngredientFormSet(self.request.POST)
        instruction_form = InstructionFormSet(self.request.POST)
        if (form.is_valid() and ingredient_form.is_valid() and instruction_form.is_valid()):
            return self.form_valid(form, ingredient_form, instruction_form)
        else:
            return self.form_invalid(form, ingredient_form, instruction_form)

    def form_valid(self, form, ingredient_form, instruction_form):
        self.object = form.save()
        ingredient_form.instance = self.object
        ingredient_form.save()
        instruction_form.instance = self.object
        instruction_form.save()
        return HttpResponseRedirect(self.get_success_url())


    def form_invalid(self, form, ingredient_form, instruction_form):
        return self.render_to_response(
            self.get_context_data(form = form,
                                  ingredient_form = ingredient_form,
                                  instruction_form = instruction_form))

以下是 base.html

中标签的代码
{% load tabs %}
        <div class="collapse navbar-collapse" id="navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="{% active request "^/$" %}"><a href="/">Home</a></li>
                <li class="{% active request "^/register/" %}"><a href="/register/">Register</a></li>
                <li class="{% active request "^/myrecipes/" %}"><a href="/myrecipes/">My Recipes</a></li>
            </ul>

            <form class="navbar-form navbar-right" role="search">
              <div class="form-group">
                <input type="text" class="form-control" placeholder="Search">
              </div>
              <button type="submit" class="btn btn-default">Submit</button>
            </form>

        </div>

urls.py中的视图

url(r'^addrecipe/', login_required(AddRecipe.as_view()),name='addrecipe', ),

我不太确定这是否是解决这个问题所需的全部细节。如果有人想进一步阐述,请随时发表评论。谢谢!

2 个答案:

答案 0 :(得分:1)

django.core.context_processors.request添加到TEMPLATE_CONTEXT_PROCESSORS

中的settings.py

其余的代码看起来很好。

答案 1 :(得分:0)

您无法像在视图中那样访问自定义模板标记中的请求对象。您可以在此处找到解决问题的方法 - Access request in django custom template tags