我在尝试加载edit_transaction视图时收到上面的错误消息。我知道错误意味着什么,但不明白它为什么会发生,因为它以前工作,仍然可以在另一个类似的页面上工作。如果需要可以提供更多代码,我想知道如果这里有一些令人眼花缭乱的东西,那么我就看不到它。
完整追溯:
回溯:
内部文件“/Users/c/.virtualenvs/django-budget/lib/python3.5/site->packages/django/core/handlers/exception.py” 42. response = get_response(request)
_get_response中的文件“/Users/c/.virtualenvs/django-budget/lib/python3.5/site->packages/django/core/handlers/base.py” 187. response = self.process_exception_by_middleware(e,request)
_get_response中的文件“/Users/c/.virtualenvs/django-budget/lib/python3.5/site-packages/django/core/handlers/base.py” 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
_wrapped_view中的文件“/Users/c/.virtualenvs/django-budget/lib/python3.5/site-packages/django/contrib/auth/decorators.py” 23. return view_func(request,* args,** kwargs)
edit_transaction中的文件“/Users/c/sites/budget/budget/views.py” 83. form = TransactionForm(instance = transaction)
异常类型:/ budget / transaction / 3 / edit /中的TypeError 异常值: init ()缺少1个必需的位置参数:'request'
从urls.py中提取:
url(r'^transaction/(?P<pk>[0-9]+)/edit/$', views.edit_transaction, name='edit_transaction'),
从views.py中提取:
@login_required
def edit_transaction(request, pk):
transaction = get_object_or_404(Transaction, pk=pk)
if request.method == "POST":
form = TransactionForm(request.POST, instance=transaction)
if form.is_valid():
transaction = form.save(commit=False)
transaction.updated = timezone.now()
transaction.save()
return redirect('view_transaction_detail', pk=transaction.pk)
else:
form = TransactionForm(instance=transaction)
return render(request, 'budget/new_transaction.html', {'form': form})
模板提取:
{% extends 'budget/base.html' %}
{% block content %}
<h2>{{ transaction.title }}</h2>
<h4>{{ transaction.date }}</h4>
<h4>Transaction type: {{ transaction.transaction_type }}</h4>
<h4>Category: {{ transaction.category }}</h4>
<h4>Budgeted amount: {{ transaction.budgeted_amount }}</h4>
<h4>Actual amount: {{ transaction.actual_amount }}</h4>
<h4>Difference: {{ difference }}</h4>
<h4>Comments: {{ transaction.comments }}</h4>
<a class="btn btn-default" href="{% url 'edit_transaction' pk=transaction.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
<a class="btn btn-default" href="{% url 'delete_transaction' pk=transaction.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
<h5><a href="{% url 'summary' %}">Back to summary</a></h5>
{% endblock %}
编辑添加TranscationForm代码
class TransactionForm(forms.ModelForm):
class Meta:
model = Transaction
fields = ('title', 'transaction_type', 'category', 'budgeted_amount', 'actual_amount', 'date', 'comments',)
#new_category field to allow you to add a new category
new_category = forms.CharField(max_length=30, required=False, label="New Category Title")
def __init__(self, request, *args, **kwargs):
super(TransactionForm, self).__init__(*args, **kwargs)
#category is now not a required field because you will use category OR new_category
self.fields['category'].required=False
#set to allow use of self.request.user to set user for category
self.request = request
def clean(self):
category = self.cleaned_data.get('category')
new_category = self.cleaned_data.get('new_category')
if not category and not new_category:
# raise an error if neither a category is selected nor a new category is entered
raise forms.ValidationError('Category or New category field is required')
elif not category:
# create category from new_category
category, created = Category.objects.get_or_create(title=new_category, defaults={'user': self.request.user})
self.cleaned_data['category'] = category
return super(TransactionForm, self).clean()
答案 0 :(得分:1)
form = TransactionForm(request=request, instance=transaction)
而不是:
form = TransactionForm(request.POST, instance=transaction)