当我的项目使用angularJS时,Django登录不起作用

时间:2015-08-23 12:01:17

标签: python html angularjs django django-authentication

我正在使用django和angularJS在登录页面上工作。但由于某些原因,即使我输入了正确的用户名和密码,我也无法登录该页面。该页面仍保留在那里并显示"在"未定义"中徘徊。以下是我的app.js文件:

var app = angular.module('exampleApp', ['ngDialog']);

app.controller('MainCtrl', function ($scope, ngDialog) {
    $scope.clickToOpen = function () {
        ngDialog.open({ template: 'view.html' });
    };
});


$(function(){
var textfield = $("input[name=user]");
            $('button[type="submit"]').click(function(e) {
                e.preventDefault();
                //little validation just to check username
                if (textfield.val() != "") {
                    //$("body").scrollTo("#output");
                    $("#output").addClass("alert alert-success animated fadeInUp").html
                    ("Welcome back " + "<span style='text-transform:uppercase'>" + textfield.val() + "</span>");
                    $("#output").removeClass(' alert-danger');
                    $("input").css({
                    "height":"0",
                    "padding":"0",
                    "margin":"0",
                    "opacity":"0"
                    });
                    //change button text
                    $('button[type="submit"]').html("continue")
                    .removeClass("btn-info")
                    .addClass("btn-default").click(function(){
                    $("input").css({
                    "height":"auto",
                    "padding":"10px",
                    "opacity":"1"
                    }).val("");
                    });

                    //show avatar
                    $(".avatar").css({
                        "background-image": "url('static/img/me.png')"
                    });
                } else {
                    //remove success mesage replaced with error message
                    $("#output").removeClass(' alert alert-success');
                    $("#output").addClass("alert alert-danger animated fadeInUp").html("sorry enter a username ");
                }
                //console.log(textfield.val());

            });
});

和我的view.py:

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.contrib import auth
from django.contrib.auth import authenticate, login
from django.core.context_processors import csrf
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render


# Create your views here.
def home(request):
    return render(request, "pages/base.html", {})

# class TemplateView(TemplateView):
#     template_name = 'pages/view.html'


def login(request):
    c = {}
    c.update(csrf(request))
    return render_to_response('pages/login.html', c)


def auth_view(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('loggedin')
    else:
        return HttpResponseRedirect('invalid')


def loggedin(request):
    return render_to_response('pages/loggedin.html', {'fullname': request.user.username})


def invalid_login(request):
    return render_to_response('pages/invalid_login.html')


def logout(request):
    auth.logout(request)
    return render_to_response('pages/logout.html')

和我的url.py

from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView


urlpatterns = [

                  url(r'^admin/', include(admin.site.urls)),
                  url(r'^$', 'app.views.home', name='home'),
                  url(r'^view$', TemplateView.as_view(template_name ='pages/view.html'), name = 'go'),
                  url(r'^friends$', TemplateView.as_view(template_name ='pages/friends.html'), name = 'friends'),
                  url(r'^firstsite$', TemplateView.as_view(template_name ='pages/firstsite.html'), name = 'firstsite'),
                  url(r'^marriage$', TemplateView.as_view(template_name ='pages/marriage.html'), name = 'marriage'),
                  url(r'^responsive', TemplateView.as_view(template_name ='pages/responsive.html'), name = 'responsive'),
                  # url(r'^login/$', 'app.views.login_user'),
                  url(r'^login', 'app.views.login'),
                  url(r'^auth$', 'app.views.auth_view'),
                  url(r'^logout', 'app.views.logout'),
                  url(r'^loggedin', 'app.views.loggedin'),
                  url(r'^invalid', 'app.views.invalid_login'),
              ] + static(settings.STATIC_URL, document_root=settings.STATIC_URL)

和我的登录页面:

<!DOCTYPE html>
<html lang="en">
{% load staticfiles %}
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Parasol.</title>

    <!-- Bootstrap core CSS -->
    <link href="{%  static 'css/bootstrap.min.css' %}" rel="stylesheet">
    <link href="{%  static 'css/style.css' %}" rel="stylesheet">

    <!-- Custom styles for this template -->

    {% block additional_styles %}
    <style>
        body{
        background:url(static/img/login.jpg) no-repeat center center fixed;
        -webkit-background-size: cover
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
        }
    </style>
    {% endblock %}

</head>
<script src="http://mymaplist.com/js/vendor/TweenLite.min.js"></script>
{% block content %}
<body>
<div class="container">
    <div class="login-container">
        <div id="output"></div>
        <div class="avatar"></div>
        <div class="form-box">
            {% if form.errors%}
            <P> Sorry Peep, dont try to cheat me.. ehaa ehaa </P>
            {% endif %}
            <form action="/auth" method="post">{% csrf_token %}

                <input name="username" type="text" placeholder="username" value="">
                <input name="password" type="password" value="" placeholder="password">
                <button class="btn btn-info btn-block login" type="submit" value="login">Login</button>
            </form>
        </div>
    </div>
</div>
</div>
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="static/js/angular.min.js"></script>
<script src="static/js/app.js"></script>
</body>
{% endblock %}

请帮我解决这个问题。先谢谢你们。

0 个答案:

没有答案