/ __init__() 处的类型错误采用 1 个位置参数,但给出了 2 个
TypeError at / __init__() takes 1 positional argument but 2 were given
我从 maxg203 复制了这段代码 https://github.com/maxg203/Django-Tutorials
我有另一个错误,但我设法解决了它们,但是当谈到这个错误时,我整整呆了 4 个小时试图解决它,直到现在我都没能解决
我还是 Django 的初学者
我的Models.py:
from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
post = models.CharField(max_length=500)
user = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Friend(models.Model):
users = models.ManyToManyField(User)
current_user = models.ForeignKey(User, related_name='owner', null=True, on_delete=models.CASCADE)
@classmethod
def make_friend(cls, current_user, new_friend):
friend, created = cls.objects.get_or_create(
current_user=current_user
)
friend.users.add(new_friend)
@classmethod
def lose_friend(cls, current_user, new_friend):
friend, created = cls.objects.get_or_create(
current_user=current_user
)
friend.users.remove(new_friend)
我的Views.py:
from django.views.generic import TemplateView
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from home.forms import HomeForm
from home.models import Post, Friend
class HomeView(TemplateView):
template_name = 'home/home.html'
def get(self, request):
form = HomeForm()
posts = Post.objects.all().order_by('-created')
users = User.objects.exclude(id=request.user.id)
friend = Friend.objects.filter(current_user=request.user)
friends = friend.users.all()
args = {
'form': form, 'posts': posts, 'users': users, 'friends': friends
}
return render(request, self.template_name, args)
def post(self, request):
form = HomeForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
text = form.cleaned_data['post']
form = HomeForm()
return redirect('home:home')
args = {'form': form, 'text': text}
return render(request, self.template_name, args)
def change_friends(request, operation, pk):
friend = User.objects.get(pk=pk)
if operation == 'add':
Friend.make_friend(request.user, friend)
elif operation == 'remove':
Friend.lose_friend(request.user, friend)
return redirect('home:home')
我的Forms.py
from django import forms
from home.models import Post
class HomeForm(forms.ModelForm):
post = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Write a post...'
}
))
class Meta:
model = Post
fields = ('post',)
My urls.py
from django.conf.urls import url
from home.views import HomeView
from . import views
urlpatterns = [
url('', views.HomeView.as_view(), name='home'),
url('connect/(?P<operation>.+)/(P<pk>\d+)/', views.change_friends, name='change_friends')
]
我的Html:
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="col-md-8">
<h2>Home</h2>
<form method="post">
{% csrf_token %}
{{ form.post }}
<br>
<button type="submit">Submit</button>
</form>
<h2>{{ text }}</h2>
{% for post in posts %}
<h1>{{ post.post }}</h1>
<p>Posted by {{ post.user.get_full_name }} on {{ post.created }}</p>
{% endfor %}
</div>
<div class="col-md-4">
<h2>Other People</h2>
{% for user in users %}
<a href="{% url 'accounts:view_profile_with_pk' pk=user.pk %}">
<h3>{{ user.username }}</h3>
</a>
{% if not user in friends %}
<a href="{% url 'home:change_friends' operation='add' pk=user.pk %}">
<button type="button" class="btn btn-success">Add Friend</button>
</a>
{% endif %}
{% endfor %}
<h2>Friends</h2>
{% for friend in friends %}
<a href="{% url 'accounts:view_profile_with_pk' pk=friend.pk %}">
<h3>{{ friend.username }}</h3>
</a>
<a href="{% url 'home:change_friends' operation='remove' pk=friend.pk %}">
<button type="button" class="btn btn-default">Remove Friend</button>
</a>
{% endfor %}
</div>
</div>
{% endblock %}
回溯:
Traceback (most recent call last):
File "C:\Users\daghe\anaconda3\envs\env\lib\site-packages\django\core\handlers\exception.py", line 47,
in inner
response = get_response(request)
File "C:\Users\daghe\anaconda3\envs\env\lib\site-packages\django\core\handlers\base.py", line 179, in
_get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
[30/Sep/2020 16:45:25] "GET / HTTP/1.1" 500 60123
我在 / 收到 TypeError
init() 接受 1 个位置参数,但给出了 2 个
最后,我通过将 _as.view 放在我的项目主页 URL
中解决了这个问题
我从 maxg203 复制了这段代码 https://github.com/maxg203/Django-Tutorials 我有另一个错误,但我设法解决了它们,但是当谈到这个错误时,我整整呆了 4 个小时试图解决它,直到现在我都没能解决 我还是 Django 的初学者
我的Models.py:
from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
post = models.CharField(max_length=500)
user = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Friend(models.Model):
users = models.ManyToManyField(User)
current_user = models.ForeignKey(User, related_name='owner', null=True, on_delete=models.CASCADE)
@classmethod
def make_friend(cls, current_user, new_friend):
friend, created = cls.objects.get_or_create(
current_user=current_user
)
friend.users.add(new_friend)
@classmethod
def lose_friend(cls, current_user, new_friend):
friend, created = cls.objects.get_or_create(
current_user=current_user
)
friend.users.remove(new_friend)
我的Views.py:
from django.views.generic import TemplateView
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from home.forms import HomeForm
from home.models import Post, Friend
class HomeView(TemplateView):
template_name = 'home/home.html'
def get(self, request):
form = HomeForm()
posts = Post.objects.all().order_by('-created')
users = User.objects.exclude(id=request.user.id)
friend = Friend.objects.filter(current_user=request.user)
friends = friend.users.all()
args = {
'form': form, 'posts': posts, 'users': users, 'friends': friends
}
return render(request, self.template_name, args)
def post(self, request):
form = HomeForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
text = form.cleaned_data['post']
form = HomeForm()
return redirect('home:home')
args = {'form': form, 'text': text}
return render(request, self.template_name, args)
def change_friends(request, operation, pk):
friend = User.objects.get(pk=pk)
if operation == 'add':
Friend.make_friend(request.user, friend)
elif operation == 'remove':
Friend.lose_friend(request.user, friend)
return redirect('home:home')
我的Forms.py
from django import forms
from home.models import Post
class HomeForm(forms.ModelForm):
post = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Write a post...'
}
))
class Meta:
model = Post
fields = ('post',)
My urls.py
from django.conf.urls import url
from home.views import HomeView
from . import views
urlpatterns = [
url('', views.HomeView.as_view(), name='home'),
url('connect/(?P<operation>.+)/(P<pk>\d+)/', views.change_friends, name='change_friends')
]
我的Html:
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="col-md-8">
<h2>Home</h2>
<form method="post">
{% csrf_token %}
{{ form.post }}
<br>
<button type="submit">Submit</button>
</form>
<h2>{{ text }}</h2>
{% for post in posts %}
<h1>{{ post.post }}</h1>
<p>Posted by {{ post.user.get_full_name }} on {{ post.created }}</p>
{% endfor %}
</div>
<div class="col-md-4">
<h2>Other People</h2>
{% for user in users %}
<a href="{% url 'accounts:view_profile_with_pk' pk=user.pk %}">
<h3>{{ user.username }}</h3>
</a>
{% if not user in friends %}
<a href="{% url 'home:change_friends' operation='add' pk=user.pk %}">
<button type="button" class="btn btn-success">Add Friend</button>
</a>
{% endif %}
{% endfor %}
<h2>Friends</h2>
{% for friend in friends %}
<a href="{% url 'accounts:view_profile_with_pk' pk=friend.pk %}">
<h3>{{ friend.username }}</h3>
</a>
<a href="{% url 'home:change_friends' operation='remove' pk=friend.pk %}">
<button type="button" class="btn btn-default">Remove Friend</button>
</a>
{% endfor %}
</div>
</div>
{% endblock %}
回溯:
Traceback (most recent call last):
File "C:\Users\daghe\anaconda3\envs\env\lib\site-packages\django\core\handlers\exception.py", line 47,
in inner
response = get_response(request)
File "C:\Users\daghe\anaconda3\envs\env\lib\site-packages\django\core\handlers\base.py", line 179, in
_get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
[30/Sep/2020 16:45:25] "GET / HTTP/1.1" 500 60123
我在 / 收到 TypeError init() 接受 1 个位置参数,但给出了 2 个
最后,我通过将 _as.view 放在我的项目主页 URL
中解决了这个问题