Python TypeError:register_user()缺少1个必需的位置参数:';请求';
我对URL模式有这样的问题。我试图回答前端的json文件,但当我运行服务器时,我遇到了一个类型错误,是否可以在没有HttpResponse或render的情况下测试我的代码? 这是我的密码 url.py 如何构造URL和json响应以成功运行服务器 views.pyPython TypeError:register_user()缺少1个必需的位置参数:';请求';,python,django,Python,Django,我对URL模式有这样的问题。我试图回答前端的json文件,但当我运行服务器时,我遇到了一个类型错误,是否可以在没有HttpResponse或render的情况下测试我的代码? 这是我的密码 url.py 如何构造URL和json响应以成功运行服务器 views.py from backend.models import RegistrationForm, AuthenticationForm from backend.responses import Responses def regist
from backend.models import RegistrationForm, AuthenticationForm
from backend.responses import Responses
def register_user(request):
if request.POST:
form = RegistrationForm(data=request.POST)
if form.is_valid():
user = form.save()
name = form.cleaned_data['username']
surname = form.cleaned_data['surname']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = User.objects.create(
first_name=name,
last_name=surname,
email=email,
is_active=False
)
user.set_password(password)
send_account_activation_email(request, user)
activate_user_account()
return Responses.ok_response(301)
else:
form = RegistrationForm(data=request.POST)
return Responses.ok_response(200)
回复.py
from django.http import JsonResponse
class Responses:
def error_response(self, error, text):
comments = [{'success': False, 'error': {'code': error, 'message': text}}]
return JsonResponse({'comments': comments})
def ok_response(self, code):
comments = [{'success': True, 'code': code}]
return JsonResponse({'comments': comments})
models.py
class RegistrationForm(forms.Form):
name_of_company = forms.CharField(label="Company name", max_length=30)
country = forms.CharField(label="Country")
city = forms.CharField(label="City/Town")
'''logo_base64 = models.ImageField'''
bio = forms.CharField(widget=forms.Textarea, label="Description")
email = forms.CharField(widget=forms.EmailInput, label="Email", max_length=150)
password = forms.CharField(label="Password", max_length=32)
check_password = forms.CharField(label="Repeat password", max_length=32)
由于您正在连接基于函数的视图,因此无需调用它。所以,去掉括号
from django.urls import path
from backend.views import register_user
urlpatterns = [
path(r'^register', register_user),
]
从django.url导入路径
从backend.views导入注册用户
URL模式=[
路径(r'^register',register\u user),
]
底部注释:我发现了这个解释了为什么你不需要用妄想症给FBV打电话。希望有帮助:)这与其他视图完全相同。您不应该在URLconf中调用视图;只需
路径(r'^register',register\u user),
。
from django.urls import path
from backend.views import register_user
urlpatterns = [
path(r'^register', register_user),
]