Django admin slug in not English


#1

in the hw app page 38,I know slug can synchronize the name in admin slug,please check the link,however,if I wanna make the name in not English,for example Chinese,how can I do it??,thank you


#2

Have you tried it? Would to know whether Django’s slugify feature handles that! Chinese characters can’t be a slug… but the Django peeps are pretty smart so I wouldn’t be surprised if there was support. Also, see this page: https://docs.djangoproject.com/es/1.9/topics/i18n/

Hope this helps!


#3

django’s slugify can handles that,just I do not know how to make it happen in admin ,for example 你好 -->ni-hao,

I can make it works on create thing in page 80 in hello web app book,I use the Django Uuslug

after I pip installed it I just “from uuslug import slugify”,it can work,however I do not know how to make it in admin,still work on this,thanks for your reply ^^ here is my code in “views.py” to create things in Chinese using uuslug ,

indent preformatted text by 4 spaces
from django.shortcuts import render,redirect
from collection.forms import ThingForm,ThingUploadForm
from collection.models import Thing,Upload
#from django.template.defaultfilters import slugify
from uuslug import slugify    #make the slug can read not English
from django.contrib.auth.decorators import login_required
from django.http import Http404


def index(request):
# this is your new view
    things = Thing.objects.all()
    return render(request, 'index.html', {'things': things,})

def thing_detail(request, slug):
# grab the object...
    thing = Thing.objects.get(slug=slug)
    uploads = thing.uploads.all()

# and pass to the template
    return render(request, 'things/thing_detail.html', { 'thing': thing,'uploads': uploads,})

@login_required
def edit_thing(request, slug):
# grab the object...
    thing = Thing.objects.get(slug=slug)
    if thing.user != request.user:
        raise Http404

# set the form we're using...
    form_class = ThingForm
    if request.method == 'POST':
# grab the data from the submitted form
        form = form_class(data=request.POST, instance=thing)
        if form.is_valid():
            # save the new data
            form.save()
            return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
    else:
        form = form_class(instance=thing)
# and render the template
    return render(request, 'things/edit_thing.html', {
        'thing': thing,
        'form': form,
    })

def create_thing(request):
    form_class = ThingForm
# if we're coming from a submitted form, do this
    if request.method == 'POST':
# grab the data from the submitted form and apply to # the form
        form = form_class(request.POST)
        if form.is_valid():
# create an instance but do not save yet
            thing = form.save(commit=False)
# set the additional details
            thing.user = request.user
            thing.slug = slugify(thing.name)
# save the object
            thing.save()
# redirect to our newly created thing
            return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
    else:
        form = form_class()
    return render(request, 'things/create_thing.html', { 'form': form,})


@login_required
def edit_thing_uploads(request, slug):
# grab the object...
    thing = Thing.objects.get(slug=slug)
# double checking just for security
    if thing.user != request.user:
        raise Http404

    form_class = ThingUploadForm
# if we're coming to this view from a submitted form,
    if request.method == 'POST':
# grab the data from the submitted form, # note the new "files" part
        form = form_class(data=request.POST,files=request.FILES, instance=thing)
        if form.is_valid():
# create a new object from the submitted form
            Upload.objects.create(
                image=form.cleaned_data['image'],
                thing=thing,
            )
            return redirect('edit_thing_uploads', slug=thing.slug)
# otherwise just create the form
    else:
        form = form_class(instance=thing)
# grab all the object's images
    uploads = thing.uploads.all()
# and render the template
    return render(request, 'things/edit_thing_uploads.html', {
        'thing': thing,
        'form': form,
        'uploads': uploads,
    })

@login_required
def delete_upload(request, id):
# grab the image
    upload = Upload.objects.get(id=id)
# security check
    if upload.thing.user != request.user:
        raise Http404
# delete the image
    upload.delete()
# refresh the edit page
    return redirect('edit_thing_uploads', slug=upload.thing.slug)

#4

Can you expand more on what specifically is happening now and what you’d like to happen in the admin?


#5

like the question I asked in stackoverflow How to generate slug from Chinese text?,I need to make the slug can generate Chinese text to like speclling,你好–>ni-hao in slug admin


#6

Cool — I think StackOverflow is the best place, I don’t think I can help you. :) Best of luck!


#7

it’s ok,this just for the discuss ^^