2017-05-07 87 views
2

因此,我正在嘗試構建一個收集Mod應用程序以學習Django Forms的Django站點。除了我是一個白癡,我不知道我在做什麼。 我試圖做的事情是收集並保存表單數據當前部分,然後嘗試在Django Admin中顯示那些數據,但我仍然沒有做過,也不知道該怎麼做。Django表單:獲得了發佈請求但數據未保存

當我填寫表格,我得到[06/May/2017 19:38:29] "POST /modapp HTTP/1.1" 200 2354

這是我forms.py:

class Application(forms.ModelForm): 



class Meta: 
    model = ModApplication 
    fields = ('discord', 'reddit', 'serverrank','finds','serverstay','active','timezone','reason','helped','help') 


def __init__(self, *args, **kwargs): 
    super(Application, self).__init__(*args, **kwargs) 
    self.fields['discord'].widget.attrs['placeholder'] = 'UnknownDeveloper#2068' 
    self.fields['reddit'].widget.attrs['placeholder'] = '/u/UnknownDeveloper' 
    self.fields['serverrank'].widget.attrs['placeholder'] = 'GEOCACHING + RANK' 
    self.fields['serverstay'].label = "How long have you been on the server" 
    self.fields['active'].label = "How active have you been on the server" 
    self.fields['timezone'].widget.attrs['placeholder'] = 'CET' 
    self.fields['reason'].label = "Why do you want to become a mod?" 
    self.fields['helped'].label = "How have you helped the server so far?" 
    self.fields['help'].label = "How will you help the server if you'll become a mod" 

models.py

class ModApplication(models.Model): 
def __unicode__(self): 
    return "Discord User: " + self.discord + " aka " + self.reddit 

reddit = models.CharField("Reddit Username", max_length=30) 
discord = models.CharField("Discord Username", max_length=30) 
serverrank = models.CharField("Server Rank", max_length=60) 
finds = models.CharField("Finds", max_length=12) 
serverstay = models.CharField("Stayed on the server for",max_length=200) 
active = models.CharField("Is Actice on Discord for", max_length=500) 
timezone = models.CharField("Timezone", max_length=20) 
reason = models.TextField("He likes to become a mod because", max_length=5000) 
helped = models.TextField("He helped by", max_length=5000) 
help = models.TextField("He'll help by", max_length=5000) 

views.py

def formtofill(request): 
form = Application() 
return render(request, 'appmod.html', {'form': form,}) 
if request.method == "POST": 
    form = Application(request.POST) 
    print(request.POST) 
    if form.is_valid(): 
     user = form.save() 
     user.discord = request.discord 
     user.reddit = request.reddit 
     user.serverrank = request.serverrank 
     user.finds = request.finds 
     user.serverstay = request.serverstay 
     user.active = request.active 
     user.timezone = request.timezone 
     user.reason = request.reason 
     user.helped = request.helped 
     user.help = request.help 
     user.save() 
     print(user) 
     messages.success(request, 'Your Mod Application has been successfully submitted!') 
     print(form) 
     return HttpResponse('Successful') 
else: 
    form_class = Application 

我搜查了一個很多,但我沒有找到任何東西。我試圖打印調試消息,但他們沒有打印任何東西。我把消息模塊,但沒有發生。 HTTPResponse只是重新加載回表單。幫助將很樂意接受。謝謝你的時間。如果我失去了一些東西,請告訴我,我來編輯自己的帖子

回答

2

這是一個驚喜,這個代碼運行在所有的,應當製作屬性錯誤,這些:

user.discord = request.discord 
    user.reddit = request.reddit 

但是,它的原因不會是因爲執行路徑從未達到正因爲如此return語句的那點

def formtofill(request): 
    form = Application() 
    return render(request, 'appmod.html', {'form': form,}) 

你糾正的代碼看起來應該像

def formtofill(request): 

    form = Application() 

    if request.method == "POST": 
     form = Application(request.POST) 
     print(request.POST) 
     if form.is_valid(): 
      user = form.save() 
      messages.success(request, 'Your Mod Application has been successfully submitted!') 
      return HttpResponseRedirect('/success_url') 
    else: 
     form_class = Application 
    return render(request, 'appmod.html', {'form': form,}) 
+0

是的。有用!!非常感謝您對不起,我是Django的白癡 – MatejMecka

1

不要保存表單前期。保存但把提交= false。 同樣在上面的答案中刪除那個return語句。

user = form.save(commit=False). 

然後創建一個實例,但它沒有寫入數據庫。現在你可以完成作業了。

user = form.save(commit=False). 
    #Now.... 
    user.discord = request.POST['discord'] 
    user.reddit = request.POST['reddit'] 
    user.serverrank = request.POST['serverrank'] 
    user.finds = request.POST['finds'] 
    user.serverstay = request.POST['serverstay'] 
    .................... 
    user.save() 
    return HttpResponse('Successful') 

相反,您試圖訪問數據的請求對象本身的屬性。表單數據包含在request.POST中,那麼你必須從那裏訪問它。

此外,

else: 
    form = Application() 

您需要實例化窗體類。

+2

抱歉,從request.POST獲取數據是錯誤的。你需要從form.cleaned_data – e4c5

+0

獲取數據是的..沒錯。我不是說這是正確的方式,但我說它也可以完成。感謝您的領導..從現在開始我會牢記這一點。 – zaidfazil