Great and easy to use scaffolding template for you Classified app made using django
DCF - django classified app
DCF - django classified app
A quick notebook for myself. And you can use it for free, now! You are welcome!
{% compress <js/css> %} link to your files here
{% endcompress %}
And thats it!
pip install https://github.com/mariocesar/sorl-thumbnail/archive/master.zip
from django.http import QueryDict
<...>
def my_delete_api(request):
if request.method == 'DELETE':
data = QueryDict(request.body)
key = data.get('key', None)
DeprecationWarning: You have no filters defined on the 'mail_admins' logging handler: adding implicit debug-false-only filter
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
url(r'^%s(?P<id>\d+)/(?P<slug>.*)/%s$' % (_('users/'), _('edit/')), app.users.edit_user, name='edit_user'),and in actual form url it look like
<form name="" action="/users/1/admin/edit/" method="post">
<form name="" action="/users/21//edit/" method="post">
<form name="" action="{% url edit_user user.id user.username|slugify %}" method="post">
from django.utils.encoding import smart_unicode
from django.template.defaultfilters import slugify
@register.filter
def slug(value):
return slugify(smart_unicode(value))
{% load extra_filters %}
<form name="" action="{% url edit_user user.id user.username|slug %}" method="post">
class CompanyInfo(models.Model):
name = models.CharField('Company name',max_length=250)
class Staff(models.Model):
company_name = models.ForeignKey(CompanyInfo)
date = models.DateField()
name = models.CharField('Name', max_length=30, )
class Relation(models.Model):
company_name = models.ForeignKey(CompanyInfo)
who = models.ForeignKey(Staff, related_name="who", blank=True)
with_whom = models.ForeignKey(Staff, related_name="with_whom", blank=True)
# -*- coding:utf-8 -*-
from models import Staff, Relation
from django.contrib import admin
from django import forms
class RelationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(RelationForm, self).__init__(*args, **kwargs)
qs = Staff.objects.filter(company_name = self.instance.company_name)
self.fields['who'].queryset = qs
self.fields['with_whom '].queryset = qs
class RelationAdmin(admin.ModelAdmin):
form = RelationForm
admin.site.register(Relation, RelationAdmin)
DATABASES = {'default': {...}, 'slave': {...}}
from django.contrib.contenttypes.models import ContentType
def run():
def do(Table):
if Table is not None:
table_objects = Table.objects.all()
for i in table_objects:
i.save(using='slave')
ContentType.objects.using('slave').all().delete()
for i in ContentType.objects.all():
do(i.model_class())
class MyModel(models.Model):
title = models.CharField(max_length=1)
def save(self, *args, **kw):
if self.pk is not None:
orig = MyModel.objects.get(pk=self.pk)
if orig.title != self.title:
print 'title is changed'
super(MyModel, self).save(*args, **kw)
python manage.py dumpdata weather.City > "weather\fixtures\initial_data.json"
python manage.py syncdbyou data will be loaded to you app model
MIDDLEWARE_CLASSES = [
'django.middleware.cache.UpdateCacheMiddleware',
...
'django.middleware.cache.FetchFromCacheMiddleware'
]
But for many sites you can't use it from the box (otherwise you get equal page for east.mycorp.com/contact.html and west.mycorp.com/contact.html).
Trick is simple, you need to do some changes in cache middleware, placed in
path to you django installation>/django/middleware/cache.py
You need overwrite two function:
from django.conf import settings
from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
from django.utils.cache import patch_response_headers, get_max_age, _i18n_cache_key_suffix
from django.utils.hashcompat import md5_constructor
from django.utils.encoding import iri_to_uri
import re
cc_delim_re = re.compile(r'\s*,\s*')
# custom function for Multisiting
def _generate_cache_header_key(key_prefix, request):
"""Returns a cache key for the header cache."""
#path = md5_constructor(iri_to_uri(request.get_full_path()))
path = md5_constructor(iri_to_uri(request.build_absolute_uri())) # patch using full path
cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
key_prefix, path.hexdigest())
return _i18n_cache_key_suffix(request, cache_key)
def _generate_cache_key(request, method, headerlist, key_prefix):
"""Returns a cache key from the headers given in the header list."""
ctx = md5_constructor()
for header in headerlist:
value = request.META.get(header, None)
if value is not None:
ctx.update(value)
#path = md5_constructor(iri_to_uri(request.get_full_path()))
path = md5_constructor(iri_to_uri(request.build_absolute_uri()))
cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % (
key_prefix, request.method, path.hexdigest(), ctx.hexdigest())
return _i18n_cache_key_suffix(request, cache_key)
def get_cache_key(request, key_prefix=None, method='GET', cache=None):
"""
Returns a cache key based on the request path and query. It can be used
in the request phase because it pulls the list of headers to take into
account from the global path registry and uses those to build a cache key
to check against.
If there is no headerlist stored, the page needs to be rebuilt, so this
function returns None.
"""
if key_prefix is None:
key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
cache_key = _generate_cache_header_key(key_prefix, request)
if cache is None:
cache = get_cache(settings.CACHE_MIDDLEWARE_ALIAS)
headerlist = cache.get(cache_key, None)
if headerlist is not None:
return _generate_cache_key(request, method, headerlist, key_prefix)
else:
return None
def learn_cache_key(request, response, cache_timeout=None, key_prefix=None, cache=None):
"""
Learns what headers to take into account for some request path from the
response object. It stores those headers in a global path registry so that
later access to that path will know what headers to take into account
without building the response object itself. The headers are named in the
Vary header of the response, but we want to prevent response generation.
The list of headers to use for cache key generation is stored in the same
cache as the pages themselves. If the cache ages some data out of the
cache, this just means that we have to build the response once to get at
the Vary header and so at the list of headers to use for the cache key.
"""
if key_prefix is None:
key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
if cache_timeout is None:
cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
cache_key = _generate_cache_header_key(key_prefix, request)
if cache is None:
cache = get_cache(settings.CACHE_MIDDLEWARE_ALIAS)
if response.has_header('Vary'):
headerlist = ['HTTP_'+header.upper().replace('-', '_')
for header in cc_delim_re.split(response['Vary'])]
cache.set(cache_key, headerlist, cache_timeout)
return _generate_cache_key(request, request.method, headerlist, key_prefix)
else:
# if there is no Vary header, we still need a cache key
# for the request.get_full_path()
cache.set(cache_key, [], cache_timeout)
return _generate_cache_key(request, request.method, [], key_prefix)
And save it to the new folder SiteCache in your django project directory.
Now add to settings.py:
MIDDLEWARE_CLASSES = (
'SiteCache.cache.UpdateCacheMiddleware',
...
'SiteCache.cache.FetchFromCacheMiddleware',
)
Congratulations, now request to cached page east.mycorp.com/contact.html and west.mycorp.com/contact.html return proper results!
from django.core.cache import cache cache.clear()