这是我的文件
urls.py 我为删除过程创建的网址路径
path('dress/<int:pk>/delete', views.DressDelete.as_view(), name="dress_delete"),
path('dress/delete/confirm', views.dress_delete_confirm, name="dress_delete_confirm"),
views.py 我在其中创建了删除视图以及将在删除完成后显示的视图以通知用户
from django.urls import reverse_lazy
from django.views.generic import DeleteView
class DressDelete (DeleteView):
model = Item
success_url = reverse_lazy('dress_delete_confirm')
@login_required
def dress_delete_confirm(request):
return render(request, 'fostania_web_app/dress_delete_confirm.html')
models.py 我要删除的表格在哪里:
class Item(models.Model):
# custom validators
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed.')
# fields
dress_name = models.ForeignKey(Name, on_delete='DO_NOTHING', blank=False, verbose_name='نوع الفستان',)
dress_rate = models.ForeignKey(Rate, on_delete='DO_NOTHING', blank=False, verbose_name='تصنيف الفستان',)
dress_size = models.ForeignKey(Size, on_delete='DO_NOTHING', verbose_name='مقاس الفستان', blank=False)
dress_color = models.CharField(max_length=50, verbose_name='لون الفستان', blank=False)
dress_image1 = models.ImageField(upload_to='documents/%Y/%m/%d', null=False, verbose_name='الصورة الأساسية للفستان',
help_text='لا يمكنك تركها فارغة',blank=False)
dress_image2 = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, verbose_name='صورة إضافية ', blank=False)
dress_image3 = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, verbose_name='صورة إضافة ', blank=False)
dress_action = models.ForeignKey(Action, on_delete='DO_NOTHING', verbose_name='الفستان معروض لل ',
help_text='للبيع او للإيجار ', blank=False)
dress_price = models.IntegerField(default=1, verbose_name='السعر', blank=False)
dress_mobile = models.CharField(max_length=20, validators=[alphanumeric], verbose_name='رقم الهاتف ', blank=False)
created_by = models.CharField(max_length=250,)
created_username = models.CharField(max_length=250, default='unknown')
created_at = models.DateTimeField(auto_now=True)
dress_active = models.BooleanField(default=False)
dress_special = models.BooleanField(default=False)
dress_town = models.ForeignKey(Town, on_delete='DO_NOTHING', verbose_name='المحافظة', blank=False)
以下是 HTML 两个文件:
item_confirm_delete.html
{% extends 'fostania_web_app/base.html' %}
{% block content %}
{% load static %}
<Br><br><br><br>
{% if request.user.email == object.created_username %}
<div class ="well well-small" align="center">
<!-- adsense -->
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- fostania-item_confirm_delete-page -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-4202417439740489"
data-ad-slot="5176371539"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<!-- end adsense -->
<form method="post">
{% csrf_token %}
<b>
<p>هل انت متأكد من رغبتك فى مسح الفستان </p>
</b>
<br>
<img src="{{ object.dress_image1.url }}" style="width: 250px;height: 250px;">
<Br>
<br>
<input class = "btn btn-danger" type="submit" value="تأكيد المسح" />
<br><br>
</form>
<a href="{% url 'home' %}"><button class="btn btn-success">رجوع للموقع</button></a>
</div>
{% else %}
<div class ="well well-small" align="center">
<b><font color="red">عفواً..لا يمكنك مسح فساتين مستخدمين غيرك</font></b><br><br>
<a href="{% url 'home' %}"><button class="btn-success">الرجوع للموقع</button></a>
</div>
{% endif %}
<!-- Placed at the end of the document so the pages load faster -->
{% endblock %}
dress_delete_confirm.html 删除后显示的HTML页面
{% extends 'fostania_web_app/base.html' %}
{% block content %}
{% load static %}
<br><br><Br><br>
<div align="center">
<!-- adsnse -->
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- fostania-dress_delete_confirm-page -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-4202417439740489"
data-ad-slot="3991406089"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<!-- end adsense -->
<div class="card border-dark mb-3" style="max-width: 18rem;">
<div class="card-header">تم مسح الفستان</div>
<div class="card-body text-dark">
<p class="card-text">
<B>لقد تم مسح الفستان بنجاح</B>
<br>
<a href="{% url 'home' %}"><button class="btn btn-success">موافــق</button></a>
</p>
</div>
</div>
</div>
{% endblock %}
这是错误和追溯
'str' object is not callable
Environment:
Request Method: POST
Request URL: https://fostania.herokuapp.com/dress/4/delete
Django Version: 2.0.5
Python Version: 3.6.4
Installed Applications:
['django_filters',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fostania_web_app.apps.FostaniaWebAppConfig']
Installed Middleware:
('whitenoise.middleware.WhiteNoiseMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/edit.py" in post
218. return self.delete(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/edit.py" in delete
213. self.object.delete()
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in delete
890. collector.collect([self], keep_parents=keep_parents)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/deletion.py" in collect
222. field.remote_field.on_delete(self, field, sub_objs, self.using)
Exception Type: TypeError at /dress/4/delete
Exception Value: 'str' object is not callable
答案 0 :(得分:6)
on_delete
参数应具有特定值,而不是简单字符串。您需要将外键更改为以下内容:
dress_town = models.ForeignKey(Town, on_delete=models.DO_NOTHING, verbose_name='المحافظة', blank=False)
您需要使用models.DO_NOTHING
代替DO_NOTHING
字符串。