我现在正在一个项目中工作,我有两台GAE服务器.一个用于开发,另一个用于生产.
所以我需要访问数据存储区来运行一些查询,因为我需要一些关于我们实体的高级信息.我在开发服务器上的方式是"URL/_ah/admin",在那里我使用交互式控制台在服务器中运行我的查询.
但显而易见的是,我需要在我的生产服务器中运行查询,我试图通过"myapp.appspot.com/admin/interactive"以相同的方式访问,但我得到了"找不到页面",我不能使用"URL/_ah/admin"访问.
因此,我寻找能够运行查询的最简单方法,现在似乎我找到了两种可行的方法,我想与您联系.
- url: /admin/.*
script: google.appengine.ext.admin.application
login: admin
然后我会访问URL"myapp.appspot.com/admin/interactive".那是对的吗?
我还没有尝试过任何东西,因为我必须确定我要做什么,因为这是一个实时项目.
非常感谢,
何塞.
我最近从1.2更新到Django 1.4.
在转换过程中,不推荐使用set_messages,现在消息API已更改为add_message.基于此页面,我应该使用以下格式:
messages.add_message(request, messages.INFO, 'Hello world.')
但是,我收到了错误global name 'request' is not defined.谁知道为什么?
这是我的代码(以粗体显示问题的行)
class InviteFriendForm(UserForm):
to_user = forms.CharField(widget=forms.HiddenInput)
message = forms.CharField(label="Message", required=False, widget=forms.Textarea(attrs = {'cols': '20', 'rows': '5'}))
def clean_to_user(self):
to_username = self.cleaned_data["to_user"]
try:
User.objects.get(username=to_username)
except User.DoesNotExist:
raise forms.ValidationError(u"Unknown user.")
return self.cleaned_data["to_user"]
def clean(self):
to_user = User.objects.get(username=self.cleaned_data["to_user"])
previous_invitations_to = FriendshipInvitation.objects.invitations(to_user=to_user, from_user=self.user)
if previous_invitations_to.count() > 0:
raise forms.ValidationError(u"Already requested friendship with %s" % to_user.username)
# check inverse
previous_invitations_from = FriendshipInvitation.objects.invitations(to_user=self.user, from_user=to_user)
if previous_invitations_from.count() > 0:
raise …Run Code Online (Sandbox Code Playgroud) 由于某种原因,当我运行时self._meta.get_all_field_names(),我得到以下输出:
['account_created', 'email', 'first_name', u'id', 'last_active',
'last_name', 'password', 'song', 'username']
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我的类中没有“歌曲”字段(但我确实有另一个名为“歌曲”的模型)。这可能是一个错误还是我做错了什么?
class user(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
email = models.EmailField()
account_created = models.DateTimeField(null=True,blank=True)
last_active = models.DateTimeField(null=True,blank=True)
def __unicode__(self):
# return "{} {} {} {} {} {} {}".format(self.first_name,self.last_name,self.username, self.password,self.email, self.account_created,self.last_active)
return ",".join(["{}:{} ".format(i,getattr(self,i)) for i in self._meta.get_all_field_names() if i != "song"])
Run Code Online (Sandbox Code Playgroud)
这是我的“歌曲”课程(如果有帮助的话):
class song(models.Model):
title = models.CharField(max_length = 128)
artist = models.CharField(max_length = 30)
submitted_by = models.ForeignKey(user,null=True,blank=True)
filename = models.CharField(max_length = …Run Code Online (Sandbox Code Playgroud) 我一直在按照说明为我的网站设置django-allauth程序包,并且一切正常,直到注册后发送确认电子邮件。一切似乎都正常,因为我注册了并且有关新帐户的信息在数据库(user表和account_emailaddress表)中,但未发送电子邮件(表中没有内容account_emailconfirmation)。EMAIL_BACKEND设置为,django.core.mail.backends.console.EmailBackend并且没有任何日志错误。
我一直在调试,并且注意到工作流程如下:
form_valid内部SignupView方法调用的complete_signup方法complete_signup 呼吁 perform_login然后,perform_login检查的第一个条件是if not user.is_active我认为在注册时始终会满足该条件,因为默认情况下新用户不处于活动状态。因此,每个注册用户都被重定向到帐户非活动页面(return HttpResponseRedirect(reverse('account_inactive')))。
我看过评论:
# Local users are stopped due to form validation checking
# is_active, yet, adapter methods could toy with is_active in a
# user_signed_up signal. Furthermore, social users should be
# stopped anyway.
Run Code Online (Sandbox Code Playgroud)
这是否意味着我应该采取一些措施使我的确认电子邮件像下一封一样发送?
@receiver(user_signed_up)
def after_user_signed_up(sender, request, user):
send_email_confirmation(request, user, signup=signup)
Run Code Online (Sandbox Code Playgroud)
我认为该程序包可以正常工作,但我不明白为什么这样做会如此,感谢您的帮助。
提前致谢。
我有一个带有字段的模型:
class MyModel(models.Model):
name = models.CharField(max_length=50)
created_by = models.ForeignKeyField(User)
Run Code Online (Sandbox Code Playgroud)
在我的管理员中:
class MyModelAdmin(admin.ModelAdmin):
list_display= ("name",)
fields = ("name",)
Run Code Online (Sandbox Code Playgroud)
在created_by添加MyModel时,我不想在管理员中使用.我想将它设置为当前用户created_by = request.user
我怎样才能做到这一点?
谢谢
我正在尝试关注tangowithdjango书,必须添加一个slug来更新类别表.但是,我在尝试迁移数据库后遇到错误.
http://www.tangowithdjango.com/book17/chapters/models_templates.html#creating-a-details-page
我没有为slug提供一个默认值,所以Django要求我提供一个,并按照书中指示我输入''.
值得注意的是,我没有使用原版书中的sqlite,而是使用了mysql.
models.py
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Category, self).save(*args, **kwargs)
class Meta:
verbose_name_plural = "Categories"
def __unicode__(self):
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __unicode__(self):
return self.title
Run Code Online (Sandbox Code Playgroud)
命令提示符
sudo python manage.py migrate
Operations to perform:
Apply all …Run Code Online (Sandbox Code Playgroud) 我从数据库中获得了用于列出客户的表格。
class CustomerForm(forms.Form):
customer = forms.ChoiceField(choices=[], required=True, label='Customer')
def __init__(self, *args, **kwargs):
super(CustomerForm, self).__init__(*args, **kwargs)
self.fields['customer'] = forms.ChoiceField(choices=[(addcustomer.company_name + ';' + addcustomer.address + ';' + addcustomer.country + ';' + addcustomer.zip + ';' + addcustomer.state_province + ';' + addcustomer.city,
addcustomer.company_name + ' ' + addcustomer.address + ' ' + addcustomer.country + ' ' + addcustomer.zip + ' ' + addcustomer.state_province + ' ' + addcustomer.city) for addcustomer in customers])
Run Code Online (Sandbox Code Playgroud)
接下来,我得到一个模态窗口,里面有一个“添加客户”表单。
问题:当我通过模式表单将新客户插入数据库(实际上正在运行)时,在重新启动本地服务器之前,CustomerForm不会是它的选择。
我需要一种在添加客户后尽快更新列表的方法。尝试过这种__init__方法,但没有运气。
我正在尝试更新我的模型,所以当通过管理员检查BooleanField时,它会更新所有其他行以重置为False(0).但是当我继续这样做时,它只返回以下TypeError:'bool'对象不可迭代
这是我的模型:
class Contact(models.Model):
name = models.CharField(max_length=255)
telephone = models.CharField(max_length=255,blank=True)
email = models.CharField(max_length=255,blank=True)
primary_contact = models.BooleanField('Primary Contact')
def __unicode__(self):
return self.name
def make_primary(self):
Contact.objects.filter(id!=self.id).update(primary_contact=False)
def save(self, *args, **kwargs):
if (self.primary_contact == True):
self.make_primary()
super(Contact, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用名为make_primary()的自定义方法在save()期间更新我的行.感觉就像我需要做的事情一样超级简单明了.我是Django的新手,所以这是一个学习曲线.
任何帮助和建议将不胜感激.
谢谢 :)
编辑:
这里要求的是我的追溯/错误如下.此外,我更新了我的代码以使用exclude而错误已更改为:'long'对象不可迭代
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/contact/contact/1/
Django Version: 1.8.2
Python Version: 2.7.6
Installed Applications:
('grappelli',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tinymce',
'adminsortable',
'taggit',
'contact')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File …Run Code Online (Sandbox Code Playgroud) 我使用AbstractBaseUser,并UserCreationForm与我的Django应用程序。通过我的应用程序注册用户时,密码以哈希格式保存并保存在数据库中。但是当我尝试使用 Django 管理站点做同样的事情时,密码以原始格式保存。
Dockerfile:
FROM python:3.6.5-jessie
MAINTAINER example-user
RUN apt-get update \
&& apt-get install -y vim
RUN mkdir require
COPY ./requirements.txt require/requirements.txt
RUN pip install -r require/requirements.txt
EXPOSE 8000
Run Code Online (Sandbox Code Playgroud)
要求.txt:
Django==2.0.2
Run Code Online (Sandbox Code Playgroud)
如上所述,我制作了一张具有名称的图像sample-image
接下来,我运行了一个容器sample-image,
docker run -ti --name sample_django -p 8001:8000 sample-image /bin/bash
在容器狂欢中,我这样命令,
root@4a947abf5ce5:/# mkdir startdir && cd startdir
root@4a947abf5ce5:/startdir# django-admin startproject sample .
root@4a947abf5ce5:/startdir# python manage.py runserver
Run Code Online (Sandbox Code Playgroud)
它返回了它:
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not …Run Code Online (Sandbox Code Playgroud) django ×9
python ×5
admin ×1
debugging ×1
django-1.7 ×1
django-admin ×1
django-forms ×1
docker ×1
email ×1
mysql ×1
web ×1