簡體   English   中英

如何在不使用Django表單的情況下讓用戶手動在Django中保存或編輯其個人資料信息

[英]How to let user save or edit his profile info in Django manually without using Django forms

我正在使用引導模板。 但是在將配置文件信息保存到數據庫中遇到了麻煩。 這是我的代碼。 我不知道我是否做對了。 但是它不起作用。

models.py

class Profile(models.Model):
  user = models.OneToOneField(User, on_delete=models.CASCADE)
  Propic = models.ImageField(upload_to="Banners/", null=True,blank=True)
  bio = models.TextField(max_length=500, blank=True)
  location = models.CharField(max_length=30, blank=True)

views.py

def profile(request):
ids = request.user.id
user = User.objects.get(pk=ids)
if request.method == "POST":
    bio = request.POST["bio"]   
    location = request.POST["location"]
    image = request.FILES['pic']

    form = user.profile(bio = bio , location = location , propic=image)
    form.save() 
    return HttpResponse("info saved")

return render(request, "dashbord/user.html", {"user": user })

在您的應用程序目錄中,創建forms.py包含:

forms.py:

from django import forms
from your-app.models import Profile # insert your currently app name 

class ProfileForm(forms.ModelForm):
      class Meta:
            model = Profile

和views.py:

from django.views import View
from your-app.models import Profile # your curr app name
from your-app.forms import ProfileForm
class UpdateProfile(View):
      model = Profile
      template_name = 'some.html'
      def get(self, request, **kwargs):
          user = Profile.objects.get(id=request.user.id)
          form = ProfileForm(initial=user.__dict__)
          return render(request, self.template_name, locals())
      def post(self, request, **kwargs):
          user = Profile.objects.get(id=request.user.id)
          os.remove(user.propic.path)
          form.ProfileForm(request.POST, request.FILES, instance=user)
          form.save()
          return redirect('/')

和您的urls.py:

 urlpatterns = [
      ....,
      path('update', UpdateProfile.as_view()),
      ...
  ]

更新模板:

<form action="update/" method="post" enctype="multipart/form-data"> <!-- this line requered, otherwise your form won't take a photo -->
    {% csrf_token %}
    <table>
        {{ form }}
    </table>

    <input type="submit" name="update" value="update">
    <input type="submit" name="cancel" value="cancel">
</form>

只是復制通過這個假設它會工作

感謝您的幫助。 我想出了怎么做。 您需要像這樣分別保存每列。

def profile(request , *args, **kwargs):
  ids = request.user.id
  user = User.objects.get(pk=ids)
  if request.method == "POST":  
     if "bio" in request.POST and "location" in request.POST:

        bio = request.POST["bio"]   
        location = request.POST["location"]

        user.profile.bio = bio 
        user.profile.location = location
        user.profile.save()

    elif "image" in request.FILES:

        image = request.FILES['image']  
        user.profile.Propic.save(image.name , image)

   return render(request, "dashbord/user.html", {"user": user })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM