我在项目admin/testing/
上这样添加了新路径urls.py
from django.views.generic.base import TemplateView
...
path('admin/', admin.site.urls),
path('admin/testing/', TemplateView.as_view(template_name='admin/testing/testing.html')),
...
,以及testing.html
上的my_app/templates/admin/testing/testing.html
,但我无需先登录即可访问admin/testing/
。在testing.html
上我有一个表单,只有当我提交表单时,它才会提示您输入我的用户名和密码。
<!-- testing.html -->
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}" />{% endblock %}
{% block coltype %}colMS{% endblock %}
{% block bodyclass %}{{ block.super }} dashboard{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block content %}
<div id="content-main">
<div id="testing-root"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/babel">
class AdminTesting extends React.Component {
...
render() {
const { state } = this;
return (
<div>
<h2>Transform Users Play Count into Ratings</h2>
<form>
Weight:<br/>
<input
type="number"
step={0.1}
name="transformWeight"
value={state.transformWeight}
onChange={this.handleChangeFraction}
/><br/>
<input style={styles.btn} type="submit" value="Submit" onClick={this.handleRatingTransformation} />
{ state.transformLoading && 'Please wait ...' }
</form>
...
</div>
)
}
}
...
ReactDOM.render(
<AdminTesting/>,
document.getElementById('testing-root'),
);
</script>
</div>
{% endblock %}
编辑:
class TemplateView(TemplateResponseMixin, ContextMixin, View):
"""
Render a template. Pass keyword arguments from the URLconf to the context.
"""
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
答案 0 :(得分:1)
您可以使用装饰器来限制访问权限
staff_member_required是满足您需求的好装饰器。 请参考this了解更多信息
from django.contrib.admin.views.decorators import staff_member_required
path('admin/testing/', staff_member_required(TemplateView.as_view(template_name='admin/testing/testing.html')))
答案 1 :(得分:1)
在您的urls.py
中,您使用的是{strong> TemplateView
中的django.views.generic.base.TemplateView
。但是它应该来自您相应的views.py
,即您定义的位置。
除此之外,请使用login_required
decorator进行身份验证,
from django.contrib.auth.decorators import login_required
from django.views.generic.base import TemplateView # remove this import
from .views import TemplateView # import "TemplateView" from your "views.py"
urlpatterns = [
path('admin/', admin.site.urls),
path('admin/testing/', login_required(
TemplateView.as_view(template_name='admin/testing/testing.html'), login_url='/admin/')),
]
答案 2 :(得分:0)
在您的TemplateView
中尝试以下代码:
class TemplateView(TemplateResponseMixin, ContextMixin, View):
"""
Render a template. Pass keyword arguments from the URLconf to the context.
"""
def get(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return render(request, 'target.html')
#target is where you want the unauthorized logger
else:
context = self.get_context_data(**kwargs)
return self.render_to_response(context)