AttributeError: 'set' object has no attribute 'items' Error (Djngo Rest Api)
AttributeError: 'set' object has no attribute 'items' Error (Djngo Rest Api)
下面的代码已经写好了,但是还是会出现下面的错误:
查看页面
from django.http.response import HttpResponse
from django.shortcuts import render
from rest_framework import serializers,status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import AccountSerializer
import requests, json
# Create your views here.
def home(request):
return render(request,'register.html')
def register(request):
return render(request,'register.html')
def login(request):
return render(request,'login.html')
@api_view(['POST'])
def registeruser(request):
if request.method == "POST":
SaveSerialize = AccountSerializer(data=request.data)
if SaveSerialize.is_valid():
SaveSerialize.save()
return Response(SaveSerialize.data,status=status.HTTP_201_CREATED)
return Response(SaveSerialize.data,status=status.HTTP_404_BAD_REQUEST)
def insertemp(request):
if request.method == "POST":
name = request.POST.get('name')
email = request.POST.get('email')
gender = request.POST.get('gender')
pass1 = request.POST.get('password1')
pass2 = request.POST.get('password2')
if pass1 == pass2:
password = pass1
address1 = request.POST.get('address1')
address2 = request.POST.get('address2')
city = request.POST.get('city')
branch = request.POST.get('branch')
address = f'{address1} {address2} {city} {branch}'
data = {
'name':name,
'email':email,
'gender':gender,
'password':password,
'address':address,
}
headers={'Content-Type: application/json'}
read = requests.post('http://127.0.0.1:8000/registeruser', json=data, headers=headers)
return render(request,'register.html')
else:
return render(request,'register.html')
这是序列化器页面:
SERIALIZER PAGE:
from rest_framework import serializers
from .models import Account
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = "__all__"
MODELS PAGE:
from django.db import models
from django.db.models.base import Model
# Create your models here.
class Account(models.Model):
name=models.CharField(max_length=500)
email=models.EmailField(max_length=254)
gender=models.CharField(max_length=20)
password=models.CharField(max_length=100)
address=models.TextField()
def __str__(self):
return self.name
URLS PAGE:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.home,name='home'),
path('register',views.register,name='register'),
path('login',views.login,name='login'),
path('registeruser',views.registeruser,name='registeruser'),
path('insertemp',views.insertemp,name='insertemp'),
]
出现以下错误:
AttributeError at /insertemp
'set' object has no attribute 'items'
Request Method: POST
Request URL: http://127.0.0.1:8000/insertemp
Django Version: 4.0
Exception Type: AttributeError
Exception Value:
'set' object has no attribute 'items'
Exception Location: H:\Projects\Advance_projects\STEPON\Login\env\lib\site-packages\requests\models.py, line 451, in prepare_headers
Python Executable: H:\Projects\Advance_projects\STEPON\Login\env\Scripts\python.exe
Python Version: 3.10.1
Python Path:
['H:\Projects\Advance_projects\STEPON\Login\login',
'C:\Users\sagar\AppData\Local\Programs\Python\Python310\python310.zip',
'C:\Users\sagar\AppData\Local\Programs\Python\Python310\DLLs',
'C:\Users\sagar\AppData\Local\Programs\Python\Python310\lib',
'C:\Users\sagar\AppData\Local\Programs\Python\Python310',
'H:\Projects\Advance_projects\STEPON\Login\env',
'H:\Projects\Advance_projects\STEPON\Login\env\lib\site-packages']
你打错了。而不是:
headers={'Content-Type: application/json'}
你需要这个:
headers = {'Content-Type': 'application/json'}
下面的代码已经写好了,但是还是会出现下面的错误:
查看页面
from django.http.response import HttpResponse
from django.shortcuts import render
from rest_framework import serializers,status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import AccountSerializer
import requests, json
# Create your views here.
def home(request):
return render(request,'register.html')
def register(request):
return render(request,'register.html')
def login(request):
return render(request,'login.html')
@api_view(['POST'])
def registeruser(request):
if request.method == "POST":
SaveSerialize = AccountSerializer(data=request.data)
if SaveSerialize.is_valid():
SaveSerialize.save()
return Response(SaveSerialize.data,status=status.HTTP_201_CREATED)
return Response(SaveSerialize.data,status=status.HTTP_404_BAD_REQUEST)
def insertemp(request):
if request.method == "POST":
name = request.POST.get('name')
email = request.POST.get('email')
gender = request.POST.get('gender')
pass1 = request.POST.get('password1')
pass2 = request.POST.get('password2')
if pass1 == pass2:
password = pass1
address1 = request.POST.get('address1')
address2 = request.POST.get('address2')
city = request.POST.get('city')
branch = request.POST.get('branch')
address = f'{address1} {address2} {city} {branch}'
data = {
'name':name,
'email':email,
'gender':gender,
'password':password,
'address':address,
}
headers={'Content-Type: application/json'}
read = requests.post('http://127.0.0.1:8000/registeruser', json=data, headers=headers)
return render(request,'register.html')
else:
return render(request,'register.html')
这是序列化器页面:
SERIALIZER PAGE:
from rest_framework import serializers
from .models import Account
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = "__all__"
MODELS PAGE:
from django.db import models
from django.db.models.base import Model
# Create your models here.
class Account(models.Model):
name=models.CharField(max_length=500)
email=models.EmailField(max_length=254)
gender=models.CharField(max_length=20)
password=models.CharField(max_length=100)
address=models.TextField()
def __str__(self):
return self.name
URLS PAGE:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.home,name='home'),
path('register',views.register,name='register'),
path('login',views.login,name='login'),
path('registeruser',views.registeruser,name='registeruser'),
path('insertemp',views.insertemp,name='insertemp'),
]
出现以下错误:
AttributeError at /insertemp
'set' object has no attribute 'items'
Request Method: POST
Request URL: http://127.0.0.1:8000/insertemp
Django Version: 4.0
Exception Type: AttributeError
Exception Value:
'set' object has no attribute 'items'
Exception Location: H:\Projects\Advance_projects\STEPON\Login\env\lib\site-packages\requests\models.py, line 451, in prepare_headers
Python Executable: H:\Projects\Advance_projects\STEPON\Login\env\Scripts\python.exe
Python Version: 3.10.1
Python Path:
['H:\Projects\Advance_projects\STEPON\Login\login',
'C:\Users\sagar\AppData\Local\Programs\Python\Python310\python310.zip',
'C:\Users\sagar\AppData\Local\Programs\Python\Python310\DLLs',
'C:\Users\sagar\AppData\Local\Programs\Python\Python310\lib',
'C:\Users\sagar\AppData\Local\Programs\Python\Python310',
'H:\Projects\Advance_projects\STEPON\Login\env',
'H:\Projects\Advance_projects\STEPON\Login\env\lib\site-packages']
你打错了。而不是:
headers={'Content-Type: application/json'}
你需要这个:
headers = {'Content-Type': 'application/json'}