【发布时间】:2022-01-03 12:07:22
【问题描述】:
我正在尝试在用户访问网站 url/用户名时进行操作,例如 http://127.0.0.1:8000/destiny/ 但它会引发 Profile() 出现意外的关键字参数“用户名”错误。我尝试在我的个人资料视图中添加用户名参数。我还尝试在用户注册时获取用户名,以便在个人资料视图中使用它,但它仍然不起作用。我是 django 新手,所以我真的不知道错误来自哪里
更新:另一个错误现已修复。但是当我想查看其他人的个人资料时,它总是返回我自己的个人资料
views.py
def UserProfile(request, username):
user = get_object_or_404(User, username=username)
profile = Profile.objects.get(user=user)
url_name = resolve(request.path).url_name
context = {
'profile':profile,
'url_name':url_name,
}
return render(request, 'userauths/profile.html', context)
#register view
def register(request):
reviews = Review.objects.filter(status='published')
info = Announcements.objects.filter(active=True)
categories = Category.objects.all()
if request.method == "POST":
form = UserRegisterForm(request.POST)
if form.is_valid():
new_user = form.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Hurray your account was created!!')
new_user = authenticate(username=form.cleaned_data['username'],
password=form.cleaned_data['password1'],
)
login(request, new_user)
return redirect('elements:home')
elif request.user.is_authenticated:
return redirect('elements:home')
else:
form = UserRegisterForm()
context = {
'reviews': reviews,
'form': form,
'info': info,
'categories': categories
}
return render(request, 'userauths/register.html', context)
models.py
class Profile(models.Model):
user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
first_name = models.CharField(max_length=1000, null=True, blank=True)
last_name = models.CharField(max_length=1000, null=True, blank=True)
email = models.EmailField(null=True, blank=True)
phone = models.IntegerField(null=True, blank=True)
bio = models.CharField(max_length=1000, null=True, blank=True)
aboutme = models.TextField(null=True, blank=True, verbose_name="About Me")
country = models.CharField(max_length=1000, null=True, blank=True)
cover_photo = models.ImageField(default='default.jpg', upload_to="profile_pic")
image = models.ImageField(default='default.jpg', upload_to="profile_pic")
joined = models.DateTimeField(auto_now_add=True, null=True)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
def __str__(self):
return f'{self.user.username} - Profile'
主项目 urls.py
from userauths.views import Profile
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('userauths.urls')),
path('<username>/', Profile, name='profile'),
更新:第一个错误已修复。这是因为我的个人资料名称是 UserProfile 而我使用的是个人资料。
现在我收到这个错误,上面写着'function' object has no attribute 'objects'
完整的追溯
System check identified no issues (0 silenced).
January 03, 2022 - 13:16:50
Django version 3.1, using settings 'dexxapikprj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /destiny/
Traceback (most recent call last):
File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Destiny\Desktop\dexxapik3\DexxaPik\dexxapikprj\userauths\views.py", line 21, in Profile
profile = Profile.objects.get(user=user)
AttributeError: 'function' object has no attribute 'objects'
[03/Jan/2022 13:16:51] "GET /destiny/ HTTP/1.1" 500 66676
[03/Jan/2022 13:16:52] "GET /favicon.ico HTTP/1.1" 301 0
Not Found: /favicon.ico/
[03/Jan/2022 13:16:52] "GET /favicon.ico/ HTTP/1.1" 404 1736
Internal Server Error: /destiny/
Traceback (most recent call last):
File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Destiny\Desktop\dexxapik3\DexxaPik\dexxapikprj\userauths\views.py", line 21, in Profile
profile = Profile.objects.get(user=user)
AttributeError: 'function' object has no attribute 'objects'
[03/Jan/2022 13:16:53] "GET /destiny/ HTTP/1.1" 500 66676
Internal Server Error: /destiny/
Traceback (most recent call last):
File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Destiny\Desktop\dexxapik3\DexxaPik\dexxapikprj\userauths\views.py", line 21, in Profile
profile = Profile.objects.get(user=user)
AttributeError: 'function' object has no attribute 'objects'
[03/Jan/2022 13:17:02] "GET /destiny/ HTTP/1.1" 500 66676
【问题讨论】:
-
您的视图名称是 UserProfile 而不是 Profile,在 urls.py 中更改吗?
-
@Sumithran 好的,我已经更改了我的视图名称,但我现在收到这个错误,说“函数”对象没有属性“对象”
-
分享完整的错误回溯
-
@Sumithran 嗨,我已经用回溯更新了问题
-
视图中从哪里导入配置文件?