簡體   English   中英

如何將表格中的數據保存到 django 型號?

[英]How to save data from form to django models?

我正在處理CS50W 項目 2 - 商業,並且一直在處理創建列表點。 我有一條路線 /create 我必須在其中獲取數據並將其保存在模型中。 我將在下面留下代碼片段。 我嘗試使用 ModelForm class,然后驗證數據並保存(),但它不保存數據。 我還在模型上運行了 shell 中的查詢,以查看數據是否存在但什么也沒有。 在代碼片段中,我將只留下有用的部分以使其看起來更清晰。 我檢查了導入(將模型導入視圖、其他模塊等)很好。 我想我在其他地方犯了一個錯誤,但我只是看不到它。 如何解決這個問題?

模型形式

class AuctionsForm(forms.ModelForm):
class Meta:
    model = Auctions
    fields = ['title' , 'description' , 'category' , 'image']

視圖.py

@login_required
def create(request):
if request.method == "POST":
    form = AuctionsForm(request.POST)
    
    if form.is_valid():
        form.save()
    else: 
        form = AuctionsForm()


return render(request, "auctions/create.html" , {"form" : AuctionsForm()} )

模型.py

class Auctions(models.Model):
CATEGORY = [
    ("No Category" , "No Category"),
    ("Fashion" , "Fashion"),
    ("Home" , "Home"),
    ("Toys" , "Toys"),
    ("Technology" , "Technology"),

]
title = models.CharField(max_length= 50)
description = models.TextField(max_length=150)
category = models.CharField(max_length = 40 , choices= CATEGORY , default="None")
image = models.URLField(default="" , blank=True)
is_active = models.BooleanField(default=True)
date = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey('auctions.User' , on_delete=models.CASCADE , related_name='user_auctions')
watchlist = models.ForeignKey('auctions.User' , on_delete=models.CASCADE , related_name='watchlist_auctions')

def __str__(self):
    return self.title

創建.html

{% extends "auctions/layout.html" %}

{% block body %}

<h1>Create Listing Here</h1>

<form method="POST" action="{% url 'index' %}">
  {% csrf_token %}

  {{form}}
  
  <input type="submit" value="Create Listing">
</form>

{% endblock %}

索引.html

{% extends "auctions/layout.html" %}

{% block body %}
<h2>Active Listings</h2>
{% for i in show%}
    <p>{{i.title}}</p>
    <p>{{i.description}}</p>
{% endfor%}
{% endblock %}

forms.py

class AuctionsForm(forms.ModelForm):
    class Meta:
       model = models.Auction
       fields = ['title', 'description', 'category', 'image']

視圖.py

def create_auction(request):
    
    if request.method == 'POST':
        form = AuctionsForm(request.POST)
    
        if form.is_valid():
            auction = form.save(commit=False)
            # do something if you but in the end
            auction.save()

            # do want you want here, or return in to creation page dunno
            return render(request, "success?idk/", {"auction": auction})
        else:
            # maybe you want to render the errors?
            # https://stackoverflow.com/questions/14647723/django-forms-if-not-valid-show-form-with-error-message
            return render(request, "auctions/create.html", {"form": form})
   
    form = AuctionForm()
    return render(request, "auction/create.html", {"form":form})

# maybe you had listing view here

使用 Auction 而不是 Auctions,Django 復數

暫無
暫無

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

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