我有一个Django模型/视图/表单在模板中正确呈现,但它没有提交输入到数据库的数据.任何有关这方面的帮助将不胜感激!
#models.py
from django.db import models
from django.forms import ModelForm
class UserRegistration(models.Model):
user_first = models.CharField(max_length=50)
user_last = models.CharField(max_length=50)
user_email = models.EmailField()
#user_fantasyhost = models.CharField(max_length=50)
def __unicode__(self):
return u'%s %s %s' % (self.user_first, self.user_last, self.user_email)
class RegForm(ModelForm):
class Meta:
model = UserRegistration
#views.py
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from acme.dc_django.models import UserRegistration
from acme.dc_django.models import RegForm
def regPage(request, id=None):
form = RegForm(request.POST or None,
instance=id and UserRegistration.objects.get(id=id))
if request.method == 'POST' and …Run Code Online (Sandbox Code Playgroud) 我正在为一个类似于Twitter转发的应用程序开发一个功能.
在模型中Item,我想添加一个相关的字段reposted_from,以引用另一个Item.我不认为我使用ForeignKey它,因为它是相同的模型,但我使用什么呢?
我在我正在构建的配置文件中有这个代码:
define("PROCESSOR_PATH", realpath(dirname(__FILE__).'/usermanager/processors'));
define("LIBRARY_PATH", PROCESSOR_PATH.'/library');
define("TEMPLATE_PATH", realpath(dirname(__FILE__).'/usermanager/assets/templates'));
Run Code Online (Sandbox Code Playgroud)
如果我在包含我的配置文件后调用这些常量中的任何一个,我什么也得不到.所以即使我这样做:
echo PROCESSOR_PATH;
Run Code Online (Sandbox Code Playgroud)
什么都没有回应.有人能指出我做错的方向吗?
我想在管理模型表单中包含一个文件字段,该表单将用于上载一个文件,然后将读取该文件,内容将用于更新同一模型中的其他字段.处理后文件本身不需要,所以我不希望模型中的文件字段只是在表单中.
我自己覆盖保存和处理表单没有问题,但我无法弄清楚如何在我的表单中包含一个不在模型中的文件字段.
刚刚完成一些CBV的工作,并想知道这是不好的风格.通常,您views.py在urls.py该视图中有一个类,在该视图中有一个URL .就像是:
views.py
from django.views.generic.list import ListView
from project.models import Contact
class ContactList(ListView):
model = Contact
Run Code Online (Sandbox Code Playgroud)
urls.py
from django.views.generic.list import ListView
from project.views import ContactList
urlpatterns = [
url(r'contacts/$', ContactList.as_view()),
]
Run Code Online (Sandbox Code Playgroud)
然后是一个显示数据的模板.
但是,如何完全跳过视图代码并在urls.py文件中完成所有操作:
from django.views.generic.list import ListView
from project.models import Contact
urlpatterns = [
url(r'contacts/$', ListView.as_view(model=Contact)),
]
Run Code Online (Sandbox Code Playgroud)
将它全部分组到urls.py文件中的样式是不是很糟糕?我的意思是,它摆脱了多余的代码,views.py所以不是那么好吗?或者这是以牺牲清晰度为代价的减少?
我的Django项目中有两个应用程序:面向公众的应用程序和管理应用程序(不是 django的内置管理站点).我希望一个域名指向公共站点,另一个域名指向管理站点.(即所以/index.html途径,例如,将指向在取决于域名其他应用的图.)每个应用程序有其自己的URL配置和它们都被包括到主URL配置.最好的方法是什么?
我想can_view在我的Django应用程序中为每个模型添加一个Meta权限.
我想把它添加到每个班级 models.py
class Meta:
permissions = [ ( "can_view", "Can view {something}".format( something = self.verbose_name ) ]
Run Code Online (Sandbox Code Playgroud)
我甚至不确定self.verbose_name在这种情况下是否会像这样工作....
这可能吗?
奖金问题:一旦我在模型中添加了权限,Meta那么我可以has_perm正确地调用它吗?喜欢
if request.user.has_perm( 'polls.can_view' ) :
# Show a list of polls.
else :
# Say "Insufficient permissions" or something.
Run Code Online (Sandbox Code Playgroud) 这是一个令人尴尬的简单问题.我正在尝试理解如何在我正在构建的第一个Django应用程序中合并一个简单的Python函数.这是我的views.py文件...
from django.shortcuts import render
from noticeboard.models import Listings
from django.views import generic
from django.template import Context
class IndexView(generic.ListView):
template_name = 'listings/index.html'
context_object_name = 'latest_listings'
def get_queryset(self):
return Listings.objects.order_by('-pub_date')
class DetailView(generic.DetailView):
model = Listings
template_name = 'listings/listings_detail.html'
context_object_name = 'listing'
Run Code Online (Sandbox Code Playgroud)
在清单模型中,我有以下字段:
listing = models.CharField(max_length=50)
Run Code Online (Sandbox Code Playgroud)
我想写一个函数来确保列表变量都是大写 - 我知道如何编写函数 - 我只是不知道如何将它合并到views.py文件中!