我正在尝试执行“添加成员”功能。基本上,当您单击“添加按钮”按钮时,成员名称将显示在此页面的某个位置。但是我收到此错误:
project_detail.html
stdout
urls.py
{% extends 'base.html' %}
{% block content %}
<p>Project {{ project_id }} detail</p>
<div class="btn btn-default oval" style="width: 10%"><a href="{% url 'project:project_edit' project_id=project_id%}">Edit</a></div>
<div class="btn btn-default oval"style="width: 10%"><a href="{% url 'project:project_delete' project_id=project_id%}">Delete</a></div>
<div class="btn btn-default oval"style="width: 10%">Members</div>
<form action="" method="POST" id="selection-form">
{% csrf_token %}
<select id="member_list">
{% for user in users %}
<option value="{{user.id}}">
{{ user.username }}
</option>
{% endfor %}
</select>
<input type="button" value="Add member" id="selection-button">
</form>
<div id="res"> </div>
{% endblock %}
{% block script %}
var url = $( '#selection-button' ).attr( 'action' );
$("#selection-button").on('click', function(e) {
e.preventDefault();
var value =$('#member_list').val();
$.ajax({
type:'POST',
url:'add_member/'+value+'/',
success:function (result) {
$("#res").append(value);
},
error:function (result) {
alert('error');
}
});
});
{% endblock %}
views.py
urlpatterns = [
path('', views.project_index, name='project_index'),
path('<int:project_id>/', views.project_detail, name='project_detail'),
path('create/', views.project_create, name='project_create'),
path('<int:project_id>/delete/', views.project_delete, name='project_delete'),
path('<int:project_id>/delete_result/', TemplateView.as_view(template_name="project/delete_result.html"), name='delete_result'),
path('<int:project_id>/edit/', views.project_edit, name='project_edit'),
path('<int:project_id>/add_member/<int:user_id>', views.add_member, name='project_add_member'),
path('<int:project_id>/task/', include('task_tracker.urls')),
path('<int:project_id>/issue/', include('issue_tracker.urls',namespace='issue_tracker')),
path('<int:project_id>/channel/', include('communication_channel.urls')),
]
project_add_member.html
def add_member(request,project_id):
if request.method=='POST':
members = User.objects.all()
serializer = UserSerializer()
return render(request,'project/project_add_member',{'project_id':project_id})
views.py
{% extends 'base.html' %}
{% block content %}
<div>success!</div>
{% endblock %}
我不知道哪里错了。好像是URL?让我知道是否需要我上传更多代码来确定问题。
答案 0 :(得分:0)
您的视图add_member
应该声明它收到的两个URL参数:
更改
def add_member(request,project_id):
...
到
def add_member(request, project_id, user_id):
project = get_object_or_404(Project, id=project_id)
user = get_object_or_404(User, id=user_id)
...