您的位置:首页 > 编程语言 > Go语言

Django(1)Tango with Django过程中的问题

2015-09-21 12:38 537 查看
Tango with Django 1.8

======================

1, url path

注意下面的<a>元素中的href属性的写法——(1)用单引号,(2)相对与域名的相对路径

def about(request):

    response = HttpResponse()

    response.write("<p>Rango says here is the about page!</p>")

    response.write("<a href='/rango/'>rango page</a>")

    return response

2,TEMPLATE_DIRS

tango教程用的是django 1.7版本,在django 1.8中,settings文件中已经没有这个变量了。

默认的模板文件搜索路径是每个app下的templates子目录,只需要把每个app的模板文件放入此目录即可,不需要修改settings

例如:rango/templates/index.html模板,引用方法为

return render(request, 'index.html', context_dict)

3,STATIC_DIRS

静态文件的目录,也和templates一样,默认在每个app下的static文件夹中,而不需要修改settings

例如:

将udfex.png放入rango/static/images目录下,就可以通过如下URL访问它
http://127.0.0.1:8000/static/images/udfex.png
问题是如果两个app下都有static/images/udfex.png,那么此url引用的是哪个文件呢?

——应该是第一个遇到的文件!根据STATICFILES_DIRS变量中的路径逐个搜索,(待测试)这就有问题了。

4,static media server是个什么东东?

注意ImageField字段有一个upload_to属性.这个属性值连接着MEDIA_ROOT设置用来提供上传文档图片的路径.例如,MEDIA_ROOT设置为<workspace>/tango_with_django_project/media/,那么upload_to=profile_imges将会使图片保存在<workspace>/tango_with_django_project/media/profile_images/目录.

5,模型和DB

model代码改变后,怎么同步到db中呢?

(1)执行makemigrations

cg@cg-e49l:/media/cg/data/5-src/web/django/mysite$ python manage.py makemigrations

Migrations for 'rango':

  0001_initial.py:

    - Create model Category

    - Create model Page

(2)执行migrates

cg@cg-e49l:/media/cg/data/5-src/web/django/mysite$ python manage.py migrate

Operations to perform:

  Synchronize unmigrated apps: staticfiles, messages

  Apply all migrations: admin, rango, contenttypes, auth, sessions

Synchronizing apps without migrations:

  Creating tables...

    Running deferred SQL...

  Installing custom SQL...

Running migrations:

  Rendering model states... DONE

  Applying rango.0001_initial... OK

6,关于slug字段

由于slug字段已存在,但没有提供默认值,所以在makemigration时会提示是否输入默认值

You are trying to add a non-nullable field 'slug' to category without a default; we can't do that (the database needs something to populate existing rows).

Please select a fix:

 1) Provide a one-off default now (will be set on all existing rows)

 2) Quit, and let me add a default in models.py

但在migrate时又报错,提示"django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category__new.slug"

其原因是:
http://stackoverflow.com/questions/27788456/integrityerror-column-slug-is-not-unique https://github.com/leifos/tango_with_django/issues/19
Listen to the error. You have the slug attribute specified as unique, but you must have the same slug value for multiple instances of your Category class.

This can easily happen if you add a unique attribute column to a table that already has data, because any constant default you use will automatically break the unique constraint.

解决办法:

1)注释掉代码中的slug相关内容,重新make migrations&migrate,从db中删除slug字段

2)重新添加slug字段,不过这一次去掉unique属性

3)修改populate脚本,调用save函数修改slug字段

4)修改model,讲slug字段改为unique属性

搞定。

7,如何修改table中的值

注意在修改之后,必须调用super(Page, self).save(*args, **kwargs)这一句,才能将改动写入DB,否则是没有生效的!

8,Form中的隐藏字段

可见字段的设置是在ModelForm的Meta类中设置fields属性来实现的.——或者使用exclude()函数来排除特定字段。

这是隐藏字段吗?——是的,都隐藏了,没有显示。

——我们可以在参数里设置widget=forms.Hiddeninput()来隐藏窗口组件。

然而可以在PageForm看到,尽管我们隐藏了字段,但是我们还是得在表单里包含字段.

如果fields排除了views,那么表单将不包含字段(尽管已经定义了)而且将不会返回给模型0值.

    views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)

    likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)

隐藏字段和使用exclude排除的字段有什么区别?

9,用户一对一和继承User类,这两种做法有什么区别?

10,用户login后,使用了HttpResponseRedirect('url'),它返回的url中,自动带了user这个模板对象,可在模板中使用!要再看一下。

11,使用DDR

修改了根目录的url为

url(r'^accounts/', include('registration.backends.simple.urls')),

为什么能够直接导航到/rango/template/registration/下的模板文件呢?

registration.backends.simple.urls里到底写了什么内容?

另外,通过以下url标签,也能定位到/rango/template/registration/下的模板文件,为什么呢?

——实际上是一个问题,这个url标签,能够解析为accounts/logout等url。

                <li><a href="{% url 'auth_logout' %}?next=/rango/">Logout</a></li>

                <li><a href="{% url 'add_category' %}">Add a New Category</a></li>

            {% else %}

                <li><a href="{% url 'registration_register' %}">Register Here</a></li>

                <li><a href="{% url 'auth_login' %}">Login</a></li>

分析:registration.backends.simple.urls中的视图,使用的是/usr/local/lib/python2.7/dist-packages/registration/templates/registration路径下的

模板,而这里的模板,默认需要一个base.html,不知什么原因,它搜到了rango/templates/base.html,就使用了这个base.html作为基础模板了。

而registration/templates/registration下的html模板,要求base.html中有一个名为“content”的block,因此只要有了这个block,就可以了看到了registration/templates/registration下的内容了。如果没有,则什么也看不到。

如果app下有一个名为registration的模板目录,那么其中的特定名称的模板,将覆盖DRR包中的原模板。(测试过,只要有registration目录,立即替换默认的页面模板)

那么可以猜测:

1)通过名为base.html的基础模板,定制了基本框架(如果要使用默认的registration模板页面,则必须有名为content的block)

2)通过名为templates/registration的目录,替换了默认的registration模板页面,这里就可以自由设计registration页面了,不需要content block。

3)每个registration模板页面,使用url标签来引用时,其名称都是固定的!

问题是:

1)如果有多个app同时存在templates/registration目录呢?

2)如知道某个功能的url标签名称?否则没法写到模板里?——这个对应的是urlpattern中的name字段。

3)如何知道模板文件(html)的名称?——对应/usr/local/lib/python2.7/dist-packages/registration/templates/registration路径下的模板文件名称即可。

12,如何注册BING API

1)API key就是“主帐户密钥”,不需要注册APP

2)“在你的市场中未提供”问题

PS1:国家或地区请勿选择‘中国’,否则会出现‘在你的市场中未提供’,如果已经注册了请在‘我的帐户’--》‘帐户信息’--》‘编辑’,选择其他的国家或地区及语言,订阅成功后,可将语言改回中文。

ps2:国家或地区不要选择worldwide系列选项,请选择最前面的选项,如”Canada (English),Australia“等等,,否则依然会出现‘在你的市场中未提供’的提示而无法订阅
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: