如何使用 kwargs 或其他东西设置多个成功网址?
我想要,取决于点击不同的成功 URL 的按钮。
保存 = 转到 DetailView
save&continue = 留在这个升级视图
save&createNew = 转到 CreateView
它应该可以像在 Django 管理员中一样工作
我有这个更新视图:
class TopicEditView(UpdateView):
fields = ['title','description',]
model = Topic
def post(self, request, *args, **kwargs):
data = request.POST.copy()
if data.get('save', False):
pass
elif data.get('save_and_continue', False):
pass
...
return UpdateView.post(self, request, *args, **kwargs)
def get_success_url(self):
return reverse('topic_detail', kwargs={'pk':self.object.pk})
Run Code Online (Sandbox Code Playgroud)
还有这个简单的模板:
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Speichern" name="save"/>
<input type="submit" value="Speichern & weiter"
name="save_and_continue"/>
</form>
Run Code Online (Sandbox Code Playgroud) 我有一个输入标签,用户必须在其中上传文件。现在,如果用户没有上传任何内容,我会在主视图中执行此操作:
if len(request.FILES) != 0:
data = request.FILES['some_file']
...do some work...
else:
return render(request, 'App/nofile.html' )
Run Code Online (Sandbox Code Playgroud)
如果没有文件,则将用户带到另一个页面,在该页面上说没有文件上传,然后使他/她返回主页。
有没有一种方法可以检查用户是否没有上传文件并且没有转到其他页面,而只是在该页面的中间显示带有消息的图片?
不只是STRING消息,而是带有消息的图片。
也许javascript是答案,但我对javascript一无所知。
在此先感谢您的帮助。
我试图在模型中调用这个覆盖保存方法:
def save(self, *args, **kwargs):
if self.done is True:
if self.averagepa is None:
pass
elif self.averagepa < 26:
self.links = 5
elif self.averagepa < 31:
self.links = 10
elif self.averagepa < 36:
self.links = 15
elif self.averagepa < 41:
self.links = 20
else:
self.links = 99
super(KW, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
如果我只是在管理面板中保存模型,这非常有效.但是,当我尝试通过./manage.py shell这样更新它:
KW.objects.filter(id=138).update()
Run Code Online (Sandbox Code Playgroud)
它不会触发它.如何通过shell更新调用保存覆盖方法?
我的Django管理面板显示的object不是self.name对象。
我在这里经历了几个类似的问题,但似乎无法解决此问题。__unicode__并__str__承担相同的结果,无论是书籍和作者。我已经更改了这些行,并在每次更改中都添加了新的作者/书籍,但没有更改。
模型
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Book(models.Model):
auto_increment_id = models.AutoField(primary_key=True)
name = models.CharField('Book name', max_length=100)
author = models.ForeignKey(Author, blank=False, null=False)
contents = models.TextField('Contents', blank=False, null=False)
def __unicode__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
我同时使用了unicode和str,结果相同。
以下是按菜单/操作显示的管理面板的屏幕截图。
第一屏
作者名单
单作者
我一直在 Crispy Forms 文档以及一般网络上搜索这个问题的答案。
Crispy Forms 可以<optgroup>使用 forms.Select 小部件在 ChoiceField 中输出吗?或者我必须将数据放入上下文中并在模板中以老式方式构建表单?
谢谢!
我有两个模型 Company 和 Actions:
from companies.models import Company
class Action(models.Model):
company = models.ForeignKey(Company, blank=True, null=True, related_name='activity', on_delete=models.CASCADE)
Run Code Online (Sandbox Code Playgroud)
然后我在 utils.py 中有一个实用程序
from .models import Action
def create_action(user, verb, target_name=None, target=None):
action = Action(user=user, verb=verb, target=target)
Run Code Online (Sandbox Code Playgroud)
我在公司模型中调用了这个实用程序def save,所以我有公司模型:
from not.utils import create_action
Run Code Online (Sandbox Code Playgroud)
所以 Action Model 将 Company Model 导入为 FK,utils 导入 Action Model,Company Model 导入 utils
现在,由于循环导入,Django 给出了一个错误:
ImportError: cannot import name 'Company'
Run Code Online (Sandbox Code Playgroud)
我在这里看到了一些 q/a 直接使用导入(没有来自)我试过但没有奏效
import not.utils as nt
nt.create_action(...)
Run Code Online (Sandbox Code Playgroud) 我是Python的新手,我在Lynda上使用视频教程来帮助我构建Social WebApp的框架.我正在尝试使用cmd python manage.py runserver从cmd 运行服务器,但是,我一直遇到此错误消息.
CMD PROMPT ERROR
Traceback (most recent call last):
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\utils\functional.py", line 36, in __get__ …Run Code Online (Sandbox Code Playgroud) 在urls.pyDjango
#urls.py
url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic')
The second part of the expression, /(?P<topic_id>\d+)/, matches an integer between two forward slashes and stores the integer value in an argument called topic_id.
Run Code Online (Sandbox Code Playgroud)
我尝试用正则表达式来理解它
In [6]: re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/")
Out[6]: ['1']
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试
In [7]: re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/").topic_id
AttributeError: 'list' object has no attribute 'topic_id'
Run Code Online (Sandbox Code Playgroud)
似乎整数没有存储topic_id,如何理解呢?
为什么这个表格没有验证?它甚至没有调用该clean()方法.
forms.py:
class SingleSampleForm(forms.Form):
sample_id = forms.CharField(label='Sample ID:')
class Meta:
fields = ('sample_id',)
def __init__(self, *args, **kwargs):
super(SingleSampleForm, self).__init__()
self.helper = FormHelper()
self.helper.layout = Layout(
Field('sample_id',
css_class="search-form-label",),
Submit('submit', 'Search sample', css_class='upload-btn')
)
self.helper.form_method = 'POST'
def clean(self):
print('CLEAN')
sample_id = self.cleaned_data['sample_id']
if sample_id:
return sample_id
raise ValidationError('This field is required')
Run Code Online (Sandbox Code Playgroud)
views.py:
class SampleView(View):
sample_form = SingleSampleForm
def get(self, request, *args, **kwargs):
sample_form = self.sample_form()
self.context = {'sample_form': sample_form,}
return render(request,
'results/single_sample_search.html',
self.context)
def post(self, request, *args, **kwargs):
self.sample_form …Run Code Online (Sandbox Code Playgroud) 当我尝试运行服务器时,出现以下错误:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/josephshenouda/anaconda3/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/Users/josephshenouda/anaconda3/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method() …Run Code Online (Sandbox Code Playgroud) django ×10
python ×9
django-1.8 ×1
django-2.0 ×1
django-urls ×1
django-views ×1
forms ×1
optgroup ×1
postgresql ×1
python-3.x ×1
valueerror ×1