【发布时间】:2020-12-11 03:08:11
【问题描述】:
这是views.py
class EditForm(forms.Form):
content = forms.CharField(label="content",widget=forms.Textarea,required=True)
def edit(request, title):
if request.method == "POST":
form = EditForm(request.POST)
if form.is_valid():
content = form.cleaned_data["content"]
util.save_entry(title, content)
return render(request, "encyclopedia/page.html", {
"title": title,
"content": util.get_entry(title)
})
else:
content = util.get_entry(title)
return render(request, "encyclopedia/edit.html", {
"form":EditForm(content)
})
这是 urls.py
path("wiki/<str:title>/edit", views.edit, name="edit")
这是util.py,根据项目,这是不应该改变的。
def list_entries():
""" 返回百科全书条目的所有名称的列表。 """
_, filenames = default_storage.listdir("entries")
return list(sorted(re.sub(r"\.md$", "", filename)
for filename in filenames if filename.endswith(".md")))
def save_entry(title, content):
""" 保存一个百科全书条目,给出它的标题和 Markdown 内容。如果已存在具有相同标题的现有条目, 它被替换了。 """
filename = f"entries/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(content))
def get_entry(title):
""" 按标题检索百科全书条目。如果没有这样 条目存在,函数返回无。 """
try:
f = default_storage.open(f"entries/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None
这是edit.html
% extends "encyclopedia/layout.html" %}
{% block title %}
Edit {{ title }}
{% endblock %}
{% block body %}
<form action="/wiki/{{ title }}/edit" method="post">
{% csrf_token %}
{{ form }}
<input type="submit">
</form>
{% endblock %}
这是错误: *
Internal Server Error: /wiki/harsha1/edit
Traceback (most recent call last):
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\harsh\Desktop\wiki\encyclopedia\views.py", line 91, in edit
"form":EditForm(content)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 994, in render
return render_value_in_context(output, context)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\template\base.py", line 973, in render_value_in_context
value = str(value)
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\utils\html.py", line 376, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\forms\forms.py", line 134, in __str__
return self.as_table()
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\forms\forms.py", line 277, in as_table
errors_on_separate_row=False,
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\forms\forms.py", line 195, in _html_output
top_errors = self.non_field_errors().copy()
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\forms\forms.py", line 306, in non_field_errors
return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\forms\forms.py", line 172, in errors
self.full_clean()
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\forms\forms.py", line 374, in full_clean
self._clean_fields()
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\forms\forms.py", line 386, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "C:\Users\harsh\Anaconda3\lib\site-packages\django\forms\widgets.py", line 258, in value_from_datadict
return data.get(name)
AttributeError: 'str' object has no attribute 'get'
[22/Aug/2020 12:18:10] "GET /wiki/harsha1/edit HTTP/1.1" 500 173523
当我转到 /title/edit 路线时,我得到 'str' 对象没有属性 'get' 属性错误。我是 django 新手,请详细说明,提前致谢。
【问题讨论】:
-
你能显示 util.py 吗?
-
我已经添加了util.py
-
@HarshaMupparaju 请提供完整的错误日志。
-
建议在get_entry函数的“return None”之前打印一些东西。
-
@mohammedwazeem 添加了完整的错误日志