我的弹出窗体收到404错误,我试图点击文字链接打开。
账户/ urls.py
url("_/user-popup/(?P<key>\w+)/$", views.user_popup_form, name="user_popup_form"),
url("_/user-popup/(?P<key>\w+)/(?P<pk>[^/]+)/$", views.user_popup_form, name="user_popup_form"),
accounts / views.py--
POPUP_FORMS = {
"profile": {
"form": forms.UserProfileForm,
"templates": {
".nav_user_name": "user/nav_user_name.html",
".profile_info": "user/profile_info.html",
".about_text": "user/about_text.html",
}
}
# etc.
}
def user_popup_form(request, key, pk=None):
"""
Handle any form with a "user" field pointing back
to the logged in user. Otherwise return a
HttpResponseForbidden response.
"""
if (not request.user.is_authenticated) or (not key in POPUP_FORMS):
raise HttpResponseForbidden()
content = {}
instance = POPUP_FORMS[key]['form']._meta.model.objects.filter(user=request.user)
if pk:
instance = instance.filter(pk=Hasher.decode(pk)[0])
else:
if not isinstance(POPUP_FORMS[key]['form']._meta.model.user.field, OneToOneField):
instance = None
instance = instance[0] if instance else None
if request.POST or request.FILES:
form = POPUP_FORMS[key]['form'](request.POST, request.FILES, instance=instance, initial={"user": request.user})
success = form.is_valid()
if success:
instance = form.save(commit=False)
instance.user = request.user
try:
instance.save()
except IntegrityError:
success = False
form.errors['__all__'] = ["Form data not unique - have you already added one of these?"]
except Exception as e:
success = False
form.errors['__all__'] = [e]
else:
models.User.objects.update()
request.user.refresh_from_db()
POPUP_FORMS[key]['form']._meta.model.objects.update()
for name, template in POPUP_FORMS[key]['templates'].items():
content[name] = render_to_string(template, {"profile": request.user.profile}, request)
else:
form = POPUP_FORMS[key]['form'](instance=instance, initial={"user": request.user})
success = False
return JsonResponse({
"form": Template("{% load crispy_forms_tags %}{% crispy form %}").render(Context({"request": request, "form": form})),
"title": capfirst(key),
"success": success,
"content": content
})
静态文件夹下的site.js
$("body").on("click tap", "a[data-user-popup]", function(e) {
e.preventDefault();
var button = $(this);
button.attr("disabled", true).find("span.fa").addClass("fa-spinner fa-spin");
var url = "/_/user-popup/" + button.attr("data-user-popup") + "/";
if(button.attr("data-item-id")) {
url += button.attr("data-item-id") + "/";
}
alert(url);
$.ajax({
url: url,
type: "GET"
}).always(function() {
button.attr("disabled", false).find("span.fa").removeClass("fa-spinner fa-spin");
}).done(function(data) {
bootbox.hideAll();
bootbox.dialog({
title: data.title,
message: data.form,
html: true,
buttons: {
"save": {
className: "btn-success",
label: "Save",
callback: function() {
var saveButton = $(".bootbox-footer .btn-success");
saveButton.attr("disabled", true).find("span.fa").addClass("fa-spinner fa-spin");
$.ajax({
url: url,
type: "POST",
data: $(".bootbox-body form").serialize(),
headers: { "X-CSRFToken": Refocus.csrfToken }
}).always(function() {
saveButton.attr("disabled", false).find("span.fa").removeClass("fa-spinner fa-spin");
}).done(function(data) {
if(data.success) {
bootbox.hideAll();
for (var key in data.content) {
$(key).html(data.content[key]);
}
} else {
$(".bootbox-body").html(data.form);
$(".modal").scrollTop(0);
}
});
return false;
}
},
"cancel": {
className: "btn-default",
label: "Cancel"
}
}
}).on('shown.bs.modal', function () {
$(".modal.fade.in .modal-body input, .modal.fade.in .modal-body textarea, .modal.fade.in .modal-body select").first().select().focus();
});
$(".modal.fade.in").scrollTop(0);
});
});
用户个人资料网址
http://127.0.0.1:8000/account/user
以及弹出窗口的代码是
{% if profile.pk == request.user.profile.pk %}
<p><a data-user-popup="profile" href="#" class="btn btn-primary"><span class="fa fa-edit"></span> Edit profile</a></p>
{% endif %}
非常感谢任何帮助。提前谢谢。
答案 0 :(得分:1)
您需要在项目urls.py文件(docs)中包含您的app urls.py文件:
from django.conf.urls import include, url
urlpatterns = [
(...)
url(r'_/', include('account.urls')),
]