Django1.0 수정 후 내용

8560 단어 djangoF#Google
     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)

좋은 웹페이지 즐겨찾기