3

I got an error, AttributeError at /accounts/login/ 'dict' object has no attribute 'status_code'. My web site's page has q login & new account registration in a page. I wrote in views.py

def login(request):
    login_form = LoginForm(request.POST)
    regist_form = RegisterForm(request.POST)
    if regist_form.is_valid():
        user = regist_form.save(commit=False)
        context = {
            'user': request.user,
            'login_form': login_form,
            'regist_form': regist_form,
        }

        return context

    if login_form.is_valid():
        user = login_form.save(commit=False)
        login(request, user)
        context = {
            'user': request.user,
            'login_form': login_form,
            'regist_form': regist_form,
        }
        return context

    context = {
        'login_form': login_form,
        'regist_form': regist_form,
    }
    return render(request, 'registration/accounts/login.html', context)

in login.html (I cannot user code format,so I decide to use code snippet)

<header class="clearfix">
          <h1 class="title">WEB SITE</h1>
    
          <ul>
            <form class="form-inline" method="post" role="form">
              {% csrf_token %}
              <div class="form-group">
                <label class="white-letter">LOGIN</label>
              </div>
              <div class="form-group">
                <label class="sr-only">USERNAME</label>
                <input id="id_username" name="username" type="text" value="" minlength="5" maxlength="12" placeholder="USERNAME" class="form-control">
              </div>
              <div class="form-group">
                <label class="sr-only">PASSWORD</label>
                <input id="id_password" name="password" type="password" value="" minlength="8" maxlength="12" placeholder="PASSWORD" class="form-control">
              </div>
              <div class="form-group">
                <button type="submit" class="btn btn-primary btn-lg" style="color:white;background-color: #F62459;border-style: none;">LOGIN</button>
                <input name="next" type="hidden"/>
              </div>
            </form>
          </ul>
  </header>

  <main>
   <div class="heading col-lg-6 col-md-12">

    <h2>NEW ACCOUNT</h2>
    <h3 class="margin-small">ALL FREE</h3>


    <form class="form-horizontal" method="POST">

      <div class="form-group-lg">

        <label for="id_username">USERNAME</label>
        
    {{ regist_form.username }}

      </div>


      <div class="form-group-lg">

        <label for="id_email">EMAIL</label>
        
     {{ regist_form.email }}
      </div>


      <div class="form-group-lg">

        <label for="id_password">PASSWORD</label>

         {{ regist_form.password1 }}

      </div>


      <div class="form-group-lg">
        <label for="id_password">PASSWORD(CONFORMATION)</label>

         {{ regist_form.password2 }}
         <p class="help-block">{{ regist_form.password2.help_text }}</p>

      </div>


      <div class="form-group-lg">
        <div class="col-xs-offset-2">
        <button type="submit" class="btn btn-primary btn-lg" style="color:white;background-color: #F62459;border-style: none;">SUBMIT</button>

        <input name="next" type="hidden"/>

        </div>
      </div>
      {% csrf_token %}

    </form>

    </div>
    </div>
  </main>

Traceback says

Traceback:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/deprecation.py" in __call__
  138.             response = self.process_response(request, response)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/middleware/common.py" in process_response
  115.         if response.status_code == 404:

Exception Type: AttributeError at /accounts/login/
Exception Value: 'dict' object has no attribute 'status_code'

I really cannot understand why this error happens. How should I fix this?

1
  • As Selcuk says returning the context is your issue. See Working with forms for a snippet to follow. Commented Oct 10, 2017 at 1:10

1 Answer 1

8

Django views must return HttpResponse instances. You are returning a dict here:

return context
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.