Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将我的用户名字段从CharField更改为ForeignKey_Python_Django_Django Models - Fatal编程技术网

Python 将我的用户名字段从CharField更改为ForeignKey

Python 将我的用户名字段从CharField更改为ForeignKey,python,django,django-models,Python,Django,Django Models,我被告知,建议对用户名字段使用ForeignKey(User),因此我现在正在尝试实现这一点。但是我在从当前Charfield()转换到ForeignKey()字段时遇到困难 下面的代码显示了如何使用当前的CharField()来协助ajax调用。它当前的工作方式是单击一个用户名,该用户名会得到所单击用户名的HTML,然后通过搜索CharField()可以.get()任何配置文件,就像这样profile=profile.objects.get(username=username\u clicke

我被告知,建议对用户名字段使用
ForeignKey(User)
,因此我现在正在尝试实现这一点。但是我在从当前Charfield()转换到ForeignKey()字段时遇到困难

下面的代码显示了如何使用当前的
CharField()
来协助ajax调用。它当前的工作方式是单击一个用户名,该用户名会得到所单击用户名的HTML,然后通过搜索
CharField()
可以.get()任何配置文件,就像这样
profile=profile.objects.get(username=username\u clicked)
。顺便说一句,该用户名所点击的HTML来自另一个
Charfield()
author
,这是我的评论模型中的一个字段,用于当有人发表评论时使用。因此,ajax调用打开了一个小的配置文件框,其中包含用户单击的一些详细信息。这是我的密码:

配置文件模型

class Profile(models.Model):
    username = models.CharField(max_length=32, default='AnonymousUser')
    age = models.IntegerField(default=0)
    points = models.IntegerField(default=0)

    def __str__(self):
        return self.username
class Comment(models.Model):
    comment_text = models.TextField(max_length=350, blank=True, null=True)
    author = models.CharField(max_length=120, blank=True)
js

$('a.username').on('click', function() {

    var username = $(this).html();
    var url = "/raise_profile/";

    $.ajax({
        type: 'GET',
        url: url,
        data: {
            username_clicked: username,
            csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()
        },
        success: function (data) {
            $('.profile_username').html(data.username);
            $('.profile_age').html(data.age);
            $('.profile_points').html(data.points);

        }
    })
});
查看

def raise_profile(request):
    username_clicked = request.GET.get('username_clicked')
    if request.is_ajax():
        profile = Profile.objects.get(username=username_clicked)
        profileAge = profile.age
        profileUsername = profile.username
        profilePoints = profile.points
        return JsonResponse({'age': profileAge, 'username': profileUsername, 'points': profilePoints})
评论模式

class Profile(models.Model):
    username = models.CharField(max_length=32, default='AnonymousUser')
    age = models.IntegerField(default=0)
    points = models.IntegerField(default=0)

    def __str__(self):
        return self.username
class Comment(models.Model):
    comment_text = models.TextField(max_length=350, blank=True, null=True)
    author = models.CharField(max_length=120, blank=True)
评论模板

<h3><a class="username">{{ i.author }}</a></h3>

为什么要玩弄CharField()。一个更好的解决方案是将author in
Comment
模型作为
用户
的外键。现在,您正在使用
标记,使用其
href
属性来保存指向作者个人资料的url。简单干净

例如

类注释(models.Model):
文本=字段
author=models.ForeignKey(用户)

现在,
可以是
作者姓名
。当用户单击此锚定标记时,获取其href并根据传递的id显示作者配置文件

编辑:

从下面的评论中,您不希望页面刷新,因此您的单击处理程序如下

$("a").on("click", "function(event){
     event.preventDefault();
     //Make an ajax call here
});

“profile”不是一个单独的URL,它只是在同一页面上显示一个小框(AJAX)和一个用户摘要,所以我不能添加
标记。但是这个小框是通过AJAX来的,对吗?那么为什么不使用url来为您提供这些数据呢?是的,但如果我这样做,添加一个
href
标记,那么它将改变页面。我需要保持在同一页上(无页面刷新)。