2013-07-23 37 views
0

設置從模型的自定義字段初始值中的ModelForm我想PestReport國家中的ModelForm自動設置,但繼續使用下面的代碼得到以下錯誤:如何在Django

TypeError at /countries/italy/pestreports/create/ 
pest_report_create() got an unexpected keyword argument 'country' 

我會最終還需要確保用戶只能添加與其個人資料所在國家/地區相同的國家/地區的有害生物報告。我正在使用django-countries

如何在下面的代碼中自動將URL的關鍵字參數設置爲表單(用戶不應該能夠選擇國家)?

urls.py

from django.conf.urls import patterns, include, url 
from .ippc.views import pest_report_create 

urlpatterns = patterns("", 
    # pest report add 
    url(r'^countries/(?P<country>[\w-]+)/pestreports/create/$', 
     view=pest_report_create, 
     name='pest-report-create'), 
) 

models.py:

from django.db import models 
from django import forms 
from django_countries.fields import CountryField 
from django.utils.translation import ugettext_lazy as _ 
from django.contrib.auth.models import User 

class IppcUserProfile(models.Model): 
    user = models.OneToOneField("auth.User") 
    country = CountryField(_("Country")) 

class PestReport(models.Model): 
    country = CountryField(_("Country")) 
    author = models.ForeignKey(User, related_name="pest_report_author") 

class PestReportForm(forms.ModelForm): 

class Meta: 
    model = PestReport 

views.py:

from .models import UserProfile, PestReport, PestReportForm 
from django.template import RequestContext 
from django.shortcuts import render_to_response, get_object_or_404, redirect 

def pest_report_create(request): 
if request.method == "POST": 

    form = form(request.POST, instance=PestReport()) 

    user = request.user 
    author_id = user.id 

    profile_user = request.profile_user 
    country = profile_user.country 

    if form.is_valid(): 
     new_pest_report = form.save(commit=False) 
     new_pest_report.author_id = author_id 
     new_pest_report.country = country 
     form.save() 
     return HttpResponseRedirect('/thank-you/') 
else: 
    form = form(instance=PestReport()) 
return render_to_response('countries/pest_report_create.html', {'form': form}, 
    context_instance=RequestContext(request)) 

修訂views.py功能(現在的作品)感謝的answerPeter Tinkler

def pest_report_create(request, country): 

    user = request.user 
    author = user 
    country=user.get_profile().country 

    form = PestReportForm(request.POST or None) 

    if request.method == "POST": 
     if form.is_valid(): 
      new_pest_report = form.save(commit=False) 
      new_pest_report.author = request.user 
      new_pest_report.author_id = author.id 
      form.save() 
      return redirect("pest-report-detail", country=country, year=new_pest_report.publish_date.strftime("%Y"), month=new_pest_report.publish_date.strftime("%m"), slug=new_pest_report.slug) 
    else: 

     form = PestReportForm(initial={'country': country}, instance=PestReport()) 

    return render_to_response('countries/pest_report_create.html', {'form': form}, 
     context_instance=RequestContext(request)) 

國家/模板/ pest_report_create.html:

{% extends "base.html" %} 

{% block main %} 

<form class="customform" method="POST" action=""> 
    {% csrf_token %} 
    {{ form.non_field_errors }} 
    {{ form.as_p }} 
    <input type="submit" value="Submit /> 
</form> 

{% endblock %} 

回答

2

您的網址有一個參數:

(?P<country>[\w-]+) 

它試圖傳遞給你的觀點,但你的視圖不接受該參數。

def pest_report_create(request): 

應該

def pest_report_create(request, country): 

你那麼想通過跨國家爲形式的初始數據:

form = PestReportForm(inital={'country': country} 

我也建議不要定義表單在模型模塊

+0

謝謝!我還設法使用更新的views.py代碼(請參閱問題中的UPDATE)將用戶的配置文件國家用作默認設置,並將PestReportForm代碼移至forms.py。 – sgriffee