基于此处给出的示例:https://github.com/django-fm/django-fm我无法基于所提供的form-view-model .py适当文件成功上传模态窗口。
这是我在models.py中的代码:
from django.db import models
class Test(models.Model):
test_one = models.CharField(max_length=200)
test_two = models.CharField(max_length=200)
def __str__(self):
'''this function returns the name of the instance'''
return self.test_one+self.test_two
这是我在forms.py中的代码:
from django import forms
from .models import Test
class TestForm(forms.ModelForm):
class Meta:
model = Test
fields = ['test_one', 'test_two',]
这是我在views.py中的代码:
from fm.views import AjaxCreateView
from .forms import TestForm
from .models import Test
class TestCreateView(AjaxCreateView):
model = Test
form_class = TestForm
template_name = 'form_example/test.html'
这是我在urls.py中的代码:
from django.contrib import admin
from django.conf.urls import url,include
from form_example.views import TestCreateView
urlpatterns =[
url(r'^create/$',TestCreateView.as_view(),name="test_create"),
]
最后是test.html中的代码:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
{% include "fm/modal.html" %}
{% block content %}{% endblock %}
<a class="fm-create" href="{% url 'test_create' %}" data-fm-head="Creating new User" data-fm-callback="appendWithAlert" >
<button class="btn btn-primary btn-sm" type="">Create new User</button></a>
<script type="text/javascript">
$(function() {
$.fm({debug: true});
});
</script>
<script type="text/javascript">
$(function() {
$.fm({
debug: true,
custom_callbacks: {
"appendWithAlert": function(data, options) {
$(options.modal_target).append(data.message);
alert("model instance created!");
}
}
});
});
</script>
</body>
</html>
如果我评论这一行
template_name = 'form_example/test.html'
现在这种奇怪是合理的吗?
答案 0 :(得分:0)
最后,我意识到出了什么问题。
我试图从相同的URL创建新的django-fm窗口。在这里,除了创建之外,我还添加了更新和删除url映射。
因此,urls.py文件定义如下:
urlpatterns = [
#### USER CRUD ####
url(r'^users/',views.user_list,name="users"),
url(r'^user/new/$',views.UserCreateView.as_view(),name="user_new"),
url(r'^user/edit/(?P<pk>\d+)/$', views.UserUpdateView.as_view(), name='user_edit'),
url(r'^user/delete/(?P<pk>\d+)/$', views.UserDeleteView.as_view(), name='user_delete'),
]
试图了解如何处理django-fm表单,我最终使用AjaxCreateView,AjaxUpdateView和AjaxDeleteView操作创建了一个整个项目,可以从以下位置访问这些操作:https://github.com/TopFlankerKiller/localagency/tree/master
在其他信息中,可以从用户的文件中找到django-fm的安装步骤:如何使用django-fm.pdf创建和操作模式窗口