简体   繁体   English

Django:如何编写一个基于 function 的视图,用于带参数的 URL

[英]Django: How to write a function based view used for URL with parameter

I am learning Django, using function based views, and I am struggling with the following: I have this path in urls.py我正在学习 Django,使用基于 function 的视图,我正在努力解决以下问题:我在 urls.py 中有这条路径

path('user/<str:username>',views.UserProjectList,name='user-projects')

that is supposed to show all the projects of the particular user (client).这应该显示特定用户(客户端)的所有项目。 In order to reach it, username should be parameter of the function based view, however I am struggling how to write such view... I have this:为了达到它,用户名应该是基于 function 的视图的参数,但是我正在努力如何编写这样的视图......我有这个:

def UserProjectList(request,username):
    user = User.objects.get(username=username) #THIS IS WRONG and should return id of the user
    #user = User.objects.filter(username=username) #also wrong

    tag_list = ProjectTagsSQL.objects.all() #ProjectTagsSQL and ProjectSQL are connected
    project_list = ProjectSQL.objects.filter(client=user) #ProjectSQL table has column client_id (pk is id in User) and table contains all the projects

    context = {
        'tagy' : tag_list,
        'projecty' : project_list
    }

    return render(request, 'home_page/user_projects.html', context) #SHOULD THE PARAMETER BE INCLUDED HERE?

I tried to inspire with the code from class based view I found on the inte.nets (thats is working for me but i didnt manage to connect it with ProjectTagsSQL as i managed in FBV, but that's a different problem) but i didnt manage我试图用我在 inte.nets 上找到的基于 class 的视图的代码来激发灵感(这对我有用,但我没有像在 FBV 中管理的那样设法将它与 ProjectTagsSQL 连接,但这是一个不同的问题)但我没有管理

class UserProjectListView(ListView):
    model = ProjectSQL
    template_name = 'home_page/user_projects.html' 
    context_object_name = 'data'

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return ProjectSQL.objects.filter(client=user)

Could someone help me how to deal with such function based view please?有人可以帮我如何处理这种基于 function 的视图吗? As this solution its not working (will return nothing for any user)由于此解决方案不起作用(不会为任何用户返回任何内容)

Here is also the ProjectSQL model (and ProjectTagsSQL model):这也是 ProjectSQL model(和 ProjectTagsSQL 模型):

class ProjectSQL(models.Model):
    id = models.AutoField(primary_key=True)
    country = models.TextField()
    city = models.TextField()
    time_added = models.DateTimeField(default=timezone.now)
    start_date = models.DateField()
    end_date = models.DateField()
    client = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        managed = False #https://docs.djangoproject.com/en/4.0/ref/models/options/
        db_table = 'project'

class ProjectTagsSQL(models.Model):
    id = models.IntegerField(primary_key=True)
    project = models.ForeignKey(ProjectSQL, on_delete=models.CASCADE)
    tag = models.ForeignKey(ProjectTagSQL, on_delete=models.CASCADE)

    class Meta:
        managed = False  # https://docs.djangoproject.com/en/4.0/ref/models/options/
        db_table = 'project_tags'

You need to write user.id so:你需要这样写user.id

from django.shortcuts import get_object_or_404

def UserProjectList(request,username):
    user = get_object_or_404(User,username=username)
    tag_list = ProjectTagsSQL.objects.all()
    project_list = ProjectSQL.objects.filter(client=user.id)

    context = {
        'tagy' : tag_list,
        'projecty' : project_list
    }

    return render(request, 'home_page/user_projects.html', context)

Also, try to check template variables' name, whether you used same or not.另外,尝试检查模板变量的名称,无论您是否使用相同的名称。

Note: Always append / at the end of every route so it should be path('user/<str:username>/'... .注意:总是 append /在每条路线的末尾,所以它应该是path('user/<str:username>/'...

Note: Function based views are generally written in snake_case so it is better to name it as user_project_list instead of UserProjectList .注意:基于 Function 的视图通常使用snake_case编写,因此最好将其命名为user_project_list而不是UserProjectList

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

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