문제

I have implemented an inline formset for this project, and whenever I go to this view, it shows the error stated in the title. Prior to this, my teammate implemented a tabbed navigation on the page using bootstrap.js. For the tabs to work, it's looking for a request for each view. I have encounted the same problem before, but fixed it since it wasn't a class-based view.

Now, it's showing the same error, but from a different view, and this time it's from a CreateView, and it doesn't really return a request that bootstrap is looking for. I'm not sure how to approach this problem so can anyone suggest a good start for me to troubleshoot this?

By the way, I'm not that experienced in Django, still learning, but enough to know the nooks and crannies.

Some of the code:

views.py (excerpt, borrowed some code from 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))

Here's the code for tabs in 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>

the view in urls.py

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

I'm not quite sure if this is all the detail needed to solve this. Feel free to comment if anyone wants further elaboration. Thanks!

도움이 되었습니까?

해결책

Add django.core.context_processors.request to your TEMPLATE_CONTEXT_PROCESSORS in settings.py

Rest of the code looks fine.

다른 팁

You do not have access to request object in your custom template tag like you do in views. You can find to resolve your issue here - Access request in django custom template tags

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top