2017-10-11 83 views
0

我試圖製作一個名爲MuyPicky的Django開發網站。這是一個網站,可以讓你根據你的挑剔找到餐館和其他東西。我目前正在製作一個表格,將餐廳添加到數據庫中。正如我在做這個,我得到這個錯誤:你如何解決這個問題:IntegrityError在/ restaurants/create? - NOT NULL約束失敗:restaurants_restaurantlocation.name

IntegrityError at /restaurants/create. 
NOT NULL constraint failed: restaurants_restaurantlocation.name 

這是爲什麼我得到錯誤

這裏是forms.py:

class RestaurantCreateForm(forms.Form): 
    title = forms.CharField() 
    location = forms.CharField(required = False) 
    category = forms.CharField(required = False) 

的form.html:

{% extends "base.html" %} 

{% block title %}Add Restaurant || {{block.super}} {% endblock %} 

{% block content %} 
    <div class="container-fluid"> 
     <div class="container"> 
      <div class="row"> 
       <div class="col-md-offset-4 col-md-4 col-md-offset-4"> 
        <h1>Add Restaurant</h1> 
        <form method="POST">{% csrf_token %} 
         <input title="Title" class="form-control" type="text" name="Title" placeholder="Title"> 
         <br> 
         <input title="Location" class="form-control" type="text" name="Location" placeholder="Location"><br> 
         <input title="Category" class="form-control" type="text" name="Category" placeholder="Category"><br> 
         <!--<input title="Save" class="form-control btn btn-info " type="submit" value="Save" >--><br> 
         <button class="btn btn-success form-control btn-md center-block" type="submit">Save</button> 
         </form> 
       </div> 
      </div> 
     </div> 
    </div> 
{% endblock %}  

來自views.py的視圖:

def restaurant_createview(request): 
    #if request.method == "GET": 
    # print("get data") 
    if request.method == "POST": 
     title = request.POST.get("title") #(request.POST["title"]) 
     location = request.POST.get("location") 
     category = request.POST.get("category") 
     obj  = RestaurantLocation.objects.create(
      name  = title, 
      location = location, 
      category = category 
     ) 
     return HttpResponseRedirect("/restaurants/") 
    template_name = "restaurants/form.html" 
    context = {} 
    return render(request, template_name, context) 

最後的urls.py:

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^/$', TemplateView.as_view(template_name="home.html")), 
    url(r'^restaurants/$', RestaurantListView.as_view()), 
    url(r'^restaurants/create/$', restaurant_createview), 
    url(r'^restaurants/(?P<slug>[\w-]+)/$', RestaurantDetailView.as_view()), 
    url(r'^contact/$', TemplateView.as_view(template_name="contact.html")), 
    url(r'^about/$',TemplateView.as_view(template_name="about.html"))] 

回答

1

你的領域都有名字TitleLocationCategory但你的Django的代碼是尋找titlelocationcategory。這些需要是一樣的。

相關問題