我在django-ajax项目中遇到问题,在该项目中,每当我尝试注册用户时,它都不会将用户信息附加到mysql数据库中,而是在运行服务器时在终端中获取此信息:>
Not Found: /sign-up/ajax-sign-up
[Time of request] " POST /sign-up/ajax-sign-up HTTP/1.1" 404 2430
此外,关于form.py上的User类,在VScode上我仍然无法解决此错误:
Class 'User' has no 'objects' member
我已经添加了pylint附加组件,以消除错误(假定是“用户”类错误),并且我重写了views.py,但没有任何效果。
这是我的forms.py脚本:
from django import forms
from django.contrib.auth import authenticate
from django.db.models import F
from project.models import User
from django.contrib.auth.hashers import make_password, check_password
from urllib.request import urlopen
from random import randint
import json, re
class Ajax(forms.Form):
args = []
user = []
def __init__(self, *args, **kwargs):
self.args = args
if len(args) > 1:
self.user = args[1]
if self.user.id == None:
self.user = "NL"
def error(self, message):
return json.dumps({ "Status": "Error", "Message": message }, ensure_ascii=False)
def success(self, message):
return json.dumps({ "Status": "Success", "Message": message }, ensure_ascii=False)
def items(self, json):
return json
def output(self):
return self.validate()
class AjaxSignUp(Ajax):
def validate(self):
try:
self.username = self.args[0]["username"]
self.password = self.args[0]["password"]
self.email = self.args[0]["email"]
except Exception as e:
return self.error("Malformed request, did not process.")
if not re.match('^[a-zA-Z0-9_]+$', self.username):
return self.error("Invalid username, must be fit [a-zA-Z0-9_]")
if not re.match('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', self.email):
return self.error("Invalid email syntax.")
if len(self.username) < 4 or len(self.username) > 20:
return self.error("Username must be between 3 and 20 characters long.")
if len(self.password) < 6 or len(self.password) > 32:
return self.error("Password must be between 6 and 32 characters long.")
if len(self.email) < 6 or len(self.email) > 140:
return self.error("Email must be between 6 and 32 characters long.")
if User.objects.filter(username=self.username).exists():
return self.error("Username already in use.")
if User.objects.filter(email=self.email).exists():
return self.error("Email address already in use.")
u = User(username=self.username, password=make_password(self.password), email=self.email)
u.save()
return self.success("Account Created!")
class AjaxLogin(Ajax):
def validate(self):
try:
self.password = self.args[0]["password"]
self.email = self.args[0]["email"]
except Exception as e:
return None, self.error("Malformed request, did not process.")
if not re.match('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', self.email):
return None, self.error("Invalid email syntax.")
if len(self.password) < 6 or len(self.password) > 32:
return None, self.error("Password must be between 6 and 32 characters long.")
if len(self.email) < 6 or len(self.email) > 140:
return None, self.error("Email must be between 6 and 32 characters long.")
if not User.objects.filter(email=self.email).exists():
return None, self.error("Email or password is incorrect.")
if not check_password(self.password, User.objects.filter(email=self.email)[0].password):
return None, self.error("Email or password is incorrect.")
u = User.objects.filter(email=self.email)[0]
return u, self.success("User logged in!")
views.py脚本:
from django.shortcuts import render, redirect
from .models import User
from .forms import *
from django.contrib.auth import authenticate, login, logout as dlogout
def ajaxsignup(request):
ajax = AjaxSignUp(request.POST)
context = {'ajax_output': ajax.output() }
return render(request, 'ajax.html', context)
def ajaxlogin(request):
ajax = AjaxLogin(request.POST)
logged_in_user, output = ajax.validate()
if logged_in_user != None:
login(request, logged_in_user)
context = {'ajax_output': output}
return render(request, 'ajax.html', context)
def signup(request):
context = {}
return render(request, 'sign-up.html', context)
def index(request):
context = {}
return render(request, 'index.html', context)
和urls.py:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('sign-up/', views.signup),
path('ajax-sign-up', views.ajaxsignup),
path('ajax-login', views.ajaxlogin),
]
无论我尝试什么,我都会继续遇到这些错误:
Not Found: /sign-up/ajax-sign-up
[Time of request] " POST /sign-up/ajax-sign-up HTTP/1.1" 404 2430
,并且用户信息未附加到数据库。用户信息应该更新,并且我确定我正确地调用了类。
非常感谢您的时间和考虑。
答案 0 :(得分:0)
我无法理解您在该Ajax类中的工作,但是该类似乎并没有被使用。您可以使用AjaxSignup和AjaxLogin来代替,这些都没有显示。
但是,尽管如此,您的错误甚至都不是来自任何此代码。正如该消息清楚地表明的那样,您正在请求一个不存在的URL。您正在请求“ / sign-up / ajax-sign-up”,但是您的注册URL只是“ / ajax-sign-up”。
(而来自pylint的警告仅仅是警告,您应该忽略它。但是,同样,您甚至不在显示的代码中的任何地方使用User。)