亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更新數據庫中的現有模型

更新數據庫中的現有模型

慕的地8271018 2022-07-26 09:32:57
大家好,我有一個問題,我想更新數據庫中的特定模型,就像我希望用戶更改他們的電話號碼和圖片......等等特定的不是所有模型,我無法在 views.py 中做到這一點,下面是我的代碼,可以幫忙嗎?models.py:    # Create your models here.    PHONE_REGEX = '^[0-9]*$'    class Profile(AbstractUser):        phone_number = models.CharField(            max_length=14,            help_text='Optional',            blank=True,            validators=[                RegexValidator(                    regex=PHONE_REGEX,                    message=                    'phone number must be only digit and from 10 to 14 digits only',                    code='invalid-phone number'),                MinLengthValidator(10)            ])        picture = models.ImageField(upload_to='upload/', blank=True, default=False)        email = models.EmailField(max_length=254,                                  unique=True,                                  help_text='Required, pls enter a valid Email')forms.py: class Change_pic_form(forms.ModelForm):    class Meta:        model = Profile        fields = ('picture', )Views.py 這里是它的片段@login_requireddef Change_picture(request):    instance = Profile.objects.get(pk=request.user.pk)    form = Change_pic_form(request.POST or None, instance=instance)    if form.is_valid():        instance = form.save(commit=False)        instance.save()        return redirect('change_pic')    else:        form = Change_pic_form(instance=instance)    context = {        'picture': instance.picture,        'form': form,        'instance': instance,    }    return render(request, 'account/change_picture.html', context)也許我沒有說清楚對不起,我的網站工作正常我已經登錄,注冊......等等,我的意思是我有用戶個人資料頁面,用戶可以在其中查看他的信息,我想要用戶,如果他想更新他的信息,如電話號碼、圖片...等
查看完整描述

3 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

您必須運行第一個makemigrations命令

python manage.py makemigrations <app_name>

它將為模型中的所有更新字段創建遷移文件,您可以在該特定應用程序的遷移文件夾中看到它。

然后你必須遷移那個特定的遷移文件......例如,你需要的模型的遷移文件0002_auto_20170808_2327然后在命令下面運行

python manage.py migrate <app_name> 0002_auto_20170808_2327


查看完整回答
反對 回復 2022-07-26
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

你有你的模型和表單,現在你需要在你的模板中呈現你的表單。從文檔中閱讀此頁面以了解如何在模板中呈現表單并在視圖中處理表單提交。



查看完整回答
反對 回復 2022-07-26
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

我找到了您必須在視圖 request.FILES 和您的表單中寫入的圖像或文件的解決方案,模板 enctype="multipart/form-data" 現在可以正常工作。


Views.py


def Change_picture(request, pk):

    prof = get_object_or_404(Profile, pk=pk)

    if request.method == "POST":

        form = Change_pic_form(request.POST,

                               request.FILES,

                               instance=request.user)

        if form.is_valid():

            form.save()

            messages.success(request, f'Your Picture updated successfully')

            return redirect('profile')

    else:

        form = Change_pic_form(instance=request.user)

    return render(request, 'account/change_picture.html', {

        'prof': prof,

        'form': form

    })

模板


<form enctype="multipart/form-data" method="POST">

            {% csrf_token %}

            <br />

            {{ form.as_p }}

            <button type="submit">Change!</button>

        </form>


查看完整回答
反對 回復 2022-07-26
  • 3 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號