本文整理汇总了Python中courses.models.Course.from_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Course.from_dict方法的具体用法?Python Course.from_dict怎么用?Python Course.from_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类courses.models.Course
的用法示例。
在下文中一共展示了Course.from_dict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_
# 需要导入模块: from courses.models import Course [as 别名]
# 或者: from courses.models.Course import from_dict [as 别名]
def import_(request):
"""Generates and accepts GET and POST requests for importing courses
If the request is get or an invalid POST, uses the template export/import.html, if valid then redirect to the newly
uploaded course.
"""
ctx = {}
if request.method == "GET":
# Return an empty form
ctx["form"] = ImportForm()
return render(request, "export/import.html", ctx)
else:
with atomic():
# Try to import the course
ctx["form"] = ImportForm(request.POST)
if not ctx["form"].is_valid():
# Form invalid? Send it right back to the user
return render(request, "export/import.html", ctx)
try:
# Attempt decode
data = decode(ctx["form"].cleaned_data["text"].replace("\r\n", "\n"))
if data is None:
raise RuntimeError("No data could be extracted from the import")
new = Course.from_dict(data, ctx["form"].cleaned_data["mode"], ctx["form"].cleaned_data["user_mode"])
return redirect(new.get_absolute_url())
except RuntimeError as e:
# Any error? Add it as an error and then show the form again
ctx["import_error"] = "Sorry, that failed to import, the error was: {}".format(e)
return render(request, "export/import.html", ctx)