Django1.0修改后内容

今天在配置django 1.02 在激活Admin 時費了不少事,google了一下,原來是django 配置有變化。  



可參考Django Step by Step 1.0.2

 The new tutorial is not finished yet.


urls.py 


以前的这么写:


from

 django.conf.urls.defaults import

 *
urlpatterns = patterns('',
    (r'^admin/

', include('django.contrib.admin.urls

'),
)
Django1.0改成:


from

 django.conf.urls.defaults import

 *
from

 django.contrib import

 admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/(.*)

', admin.site.root),
)

=========================================================================
models.py




以前:


class

 MyTag

(models.Model):
    name = models.CharField(blank=True, max_length=100)

    class

 Meta

:
        verbose_name = 'MyTag

'
        verbose_name_plural = 'MyTag

'

    class

 Admin

:
        pass



    def

 __unicode__

(self):
        return

 self.name

class

 MyEntry

(models.Model):
    title = models.CharField(blank=True, max_length=100)
    content = models.TextField(blank=True)
    pub_date = models.DateTimeField(blank=True, default=datetime.datetime.now)
    tags = models.ManyToManyField(MyTag, blank=True)

    class

 Meta

:
        verbose_name = 'Entry

'
        verbose_name_plural = 'Entries

'
        ordering = ['-pub_date

']

    class

 Admin

:
        list_display = ['title

', 'pub_date

']
        search_fields = ['title

', 'content

']
        js = ['/static/js/customize.js

']

    def

 __unicode__

(self):
        return

 self.title


Django1.0改成:


class

 MyTag

(models.Model):
    name = models.CharField(blank=True, max_length=100)

    class

 Meta

:
        verbose_name = 'MyTag

'
        verbose_name_plural = 'MyTag

'

    def

 __unicode__

(self):
        return

 self.name

class

 MyEntry

(models.Model):
    title = models.CharField(blank=True, max_length=100)
    content = models.TextField(blank=True)
    pub_date = models.DateTimeField(blank=True, default=datetime.datetime.now)
    tags = models.ManyToManyField(MyTag, blank=True)

    class

 Meta

:
        verbose_name = 'Entry

'
        verbose_name_plural = 'Entries

'
        ordering = ['-pub_date

']

    def

 __unicode__

(self):
        return

 self.title

admin.py



from

 django.contrib import

 admin
from

 models import

 MyTag, MyEntry

class

 MyEntryAdmin

(admin.ModelAdmin):
    list_display = ['title

', 'pub_date

']
    search_fields = ['title

', 'content

']

    class

 Media

:
        js = ['/static/js/customize.js

']

admin.site.register(MyTag)
admin.site.register(MyEntry, MyEntryAdmin)

======================================
forms
以前:


from

 django import

 newforms as

 forms
Django1.0 改成:


from

 django import

 forms

======================================
models.py



class

 MyModel

(models.Model):
    myphoto = models.ImageField(upload_to="img/uploads

")

以前:


myinstance = MyModel.objects.all()[0]
photo_url = myinstance.get_myphoto_url()

Django1.0改成:


myinstance = MyModel.objects.all()[0]
photo_url = myinstance.myphoto.url

======================================
Forms:
class

 MyUploadForm

(forms.Form):
    target_file = forms.FileField(widget=forms.FileInput)

以前:


form = MyUploadForm(request.POST, request.FILES)
if

 form.is_valid():
    target_file = form.cleaned_data['target_file

']
    f = open(target_file.filename, 'wb

')
    f.write(target_file.content)
Django1.0改成:


form = MyUploadForm(request.POST, request.FILES)
if

 form.is_valid():
    target_file = form.cleaned_data['target_file

']
    f = open(target_file.name, 'wb

')
    for

 chunk in

 target_file.chunks():
        f.write(chunk)
    f.close()


======================================
Signal
以前:


from

 django.db.models.signals import

 pre_save
from

 django.dispatch import

 dispatcher

def

 update

(signal, sender, instance, **kwds):
    instance.pub_date = datetime.datetime.now()
dispatcher.connect(update, pre_save, MyEntry)

Django1.0改成:


from

 django.db.models.signals import

 pre_save

def

 update

(signal, sender, instance, **kwds):
    instance.pub_date = datetime.datetime.now()
pre_save.connect(receiver=update, sender=MyEntry)

你可能感兴趣的:(django,F#,Google)