简体   繁体   English

我想在用户单击传递到个人资料页面时创建 url -- NoReverseMatch at /home

[英]i want create url when user click pass to profile page -- NoReverseMatch at /home

i want create url when user click pass to profile page当用户点击传递到个人资料页面时,我想创建 url

在此处输入图像描述

++++ models ++++模型

class profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    music = models.CharField(max_length=50)
    skils = models.CharField(max_length=50)
    search = models.CharField(max_length=50)
    posts = models.CharField(max_length=50)
    boi = models.TextField()
    img = models.ImageField(upload_to="profile-img")

    def __str__(self):
        #return self.user or 'User'
        return str(self.id)

def create_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = profile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=User)

+++ views +++ 意见

def home(request): 
    return render(request, 'main-frond.html')

def profile_edit(request, id):
    pro_edit = profile.objects.get(id=id)
    if request.method == 'POST':
        user_form = User_Form(request.POST, request.FILES, instance=pro_edit)
        if user_form.is_valid():
            user_form.save()
            return redirect('profile_views')
    else:
        user_form = User_Form(instance=pro_edit)
    context = {'user_form' : user_form}
    return render(request, 'profile_edit.html', context)

+++url +++网址

    path('home', views.home, name="home"),
    path('profile/<int:id>/edit', views.profile_edit, name="profile_edit")

+++ html +++ html

<a href="{% url 'profile_views' %}">profile</a>

django give me problem django 给我问题

Reverse for 'profile_views' with no arguments not found. 1 pattern(s) tried: ['profile/(?P<id>[0-9]+)\\Z']

i want create url when user click pass to profile page当用户点击传递到个人资料页面时,我想创建 url

It appears that you are trying to create a URL for a Django application that is not properly configured.您似乎正在尝试为未正确配置的 Django 应用程序创建 URL。 The error message "Reverse for 'profile_views' with no arguments not found" suggests that you have not provided the correct arguments in the URL pattern for the 'profile_views' view.错误消息“Reverse for 'profile_views' with no arguments not found”表明您没有在“profile_views”视图的 URL 模式中提供正确的 arguments。

It looks like in the URL pattern, you are expecting an argument 'id' to be passed to the view but it is not being passed.看起来在 URL 模式中,您期望将参数“id”传递给视图,但它没有被传递。 Make sure that you are passing the 'id' argument in the URL when redirecting the user to the profile page.确保在将用户重定向到个人资料页面时在 URL 中传递“id”参数。

For example, you can pass the id in the URL like this:例如,您可以像这样传递 URL 中的 id:

path('profile/<int:id>/', views.profile_views, name='profile_views'),

and then in your views.py然后在你的 views.py 中

def profile_views(request, id):
    profile = get_object_or_404(Profile, pk=id)
    return render(request, 'profile.html', {'profile': profile})

Then when redirecting the user to the profile page, you can pass the 'id' of the profile like this:然后,当将用户重定向到个人资料页面时,您可以像这样传递个人资料的“id”:

return redirect('profile_views', id=profile.id)

You should also make sure that the profile_views function is defined in views.py and that it is imported in your urls.py file.您还应该确保 profile_views function 已在 views.py 中定义,并且已导入到您的 urls.py 文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM