1

Hi Stackoverflow community!

This marks a long time followers' first post! I have almost always found an answer to my seemingly impossible questions here, but the time has come, I rage-quitted google searching and decided to post here! All help is appreciated!

As a Django noob, I am currently struggling to design an app that contains only 2 types of URLs (think: appear.in) So basically, homepage at localhost/ has a form for folder name input and create folder button.

If localhost/FOLDER1/ does exist, the folder will be expanded, otherwise (i.e. localhost/NOTEXISTING/) it will redirect to home with a rendered HTML text input with value='NOTEXISTING'. Supplementary Contents table provides details on ForeignKey(Folder)

My current urlpatters are:

url(r'(\w+)/$', ShowFolder), #Trying to capture the folder name
url(r'^$', Homepage),        #Trying to capture blank URL -> home

views.py contains:

def Homepage(request):
    t = get_template('home.html')
    html=t.render(Context({'foldername': foldername}))
    return HttpResponse(html)

def ShowFolder(request, foldername):
    try:
         folder = Folder.objects.get(name=foldername)
         html=t.render(Context({'folder': folder }))
         return HttpResponse(html)
    except Folder.DoesNotExist:
         t = get_template('home.html')
         fradd = ("Folder not found, wanna create one?")
         html=t.render(Context({'foldername': foldername}))
         return HttpResponse(fradd + html)

HTML Form looks like this:

 <form >
<b>Folder Name: </b>
<input type="text" name="FodlerName" value=" {{ foldername }} ">
<input type="button" id="btn" value="Create Folder" name="btn">
 </form> 

So what really halted my progress is my lack of knowledge on page redirection, and capturing submit events.

What I would like to right now is to get folder name from input, create a folder with that name and redirect to localhost/NEWFOLDER without any more URL modification in the process. I have tried countless tutorials on forms and HTML examples with JS an JQuery, albeit without success.

Any help would be more than appreciated! Thank you sincerely in advance!

1 Answer 1

1

I'm not entirely certain what view is supposed to be doing what here. But just getting the folder name from the form post is easy, via request.POST['FolderName'], assuming you're submitting the form with POST which you're not but should be.

And redirecting to that name is also simple, using the redirect shortcut:

from django.shortcuts import redirect

folder_name = request.POST['FolderName']
return redirect('ShowFolder', (folder_name,))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.