Python Forum
Help finishing my Wikipedia page
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help finishing my Wikipedia page
#1
My issue right now is getting the view to complete both the rendering of the function that edits the entry but then also redirect the user back to the wiki/<title> webpage. I can't figure out how to make it return those 2 things. I want it to save the entry first, then go back to the original page that was edited. How can i do that?

Here is my code in question:

def save(request):
    title = request.POST.get("title")
    content = request.POST.get("content")
    return render(request, "encyclopedia/save.html", {
            "entries": util.save_entry(title, content)
        })
how is it possible that I can make it return the save_entry function but also return back to the original page? What is the syntax for including two things?

I can change the encyclopaedia/save.html to encyclopedia/wiki/<title> but then it gives me an error that save is not a valid page. What's the best way to make sure it runs the save.html briefly (that says "record save") then reloads back to the original page? Thanks!
Reply
#2
It's really not clear what you're trying to do. Why does the return value of the call to save_entry need to be passed to render? Is "encyclopedia/save.html" a template that needs that value? If so, why do you need to redirect? On the flip side, if that isn't a template that needs rendering and you just need to do a redirect after doing the save, why are you calling render?
Reply
#3
I thought that was the syntax of all views? Why does it matter if I pass it or not? I'm not using it on save.html.

So I shouldn't call render? Why not? What should I do instead?

would i just use "return util.save_entry(title, content)"? What page should it load?

the original page where I did the editing was wiki/<title> How can I get the title on the save page because I need to use that title to go back to the original page. How else can I get the title variable?

OK I used your advice and figured it out. I changed it to:

def save(request):
    title = request.POST.get("title")
    content = request.POST.get("content")
    util.save_entry(title, content)
    return render(request, "encyclopedia/wiki.html", {
        "entries": util.get_entry(title)
    })
Probably not the most efficient but it works!

OK I'm on the final step! Now I have to change the markup to HTML when shown to the user. I'm going to fiddle around with this later tonight. Any tips as I do it?

Thanks
Reply
#4
(Jul-07-2020, 03:20 AM)card51shor Wrote: I thought that was the syntax of all views?

Well, think about it. Not all views need to render a template, do they? Examples:

- The response from an API endpoint, which as you know by now will typically be XML or JSON.
- Redirecting to another page. This is usually done by setting the appropriate status code and the Location header.

Quote:So I shouldn't call render? Why not? What should I do instead?

Quote:Now I have to change the markup to HTML when shown to the user. I'm going to fiddle around with this later tonight. Any tips as I do it?

These two things make me a bit worried. You seem to be averse to reading documentation. For the former, OK you've figured out a solution by now, but the docs do tell you how to create HTTP responses. I'm not a Django user, but looking at the "Part 3: Views and templates" page linked to from here, there's useful information (I found what I wanted under "Write views that actually do something"). For the latter, you've been advised to use a library to do that, so you need to consult its documentation. It's critical that you use documentation, tutorials etc. to figure stuff out - there isn't always going to be someone on this forum, say, who knows the ins and outs of whatever library you're using. I don't know if you're planning on doing development as a job, but even in that case, you'd have to figure stuff out because it wouldn't really be possible to wait an unspecified amount of time to get an answer. Hint: choose libraries that are well documented and maintained!
Reply
#5
Quote:Well, think about it. Not all views need to render a template, do they? Examples:

- The response from an API endpoint, which as you know by now will typically be XML or JSON.
- Redirecting to another page. This is usually done by setting the appropriate status code and the Location header.

OK that makes sense except can u show me an example of a redirect? I'm not sure what you mean by the status code and Location header.

What do you mean by Use a library? I don't always learn by seeing code. I have to see it in action and break it down. I find conflicting information online and it's hard to get solid syntax sometimes so I come to you guys to help stimulate my mind.

By use a libray you mean the markdown2 import I have to make? OK I'll consult that and look into it. Sometimes I ask questions beforehand because I get frustrated trying to follow the instructions - and they aren't always fully complete - something always goes wrong.

And it helps me to ask questions first so when I'm ready to go at it they are answered.

My code is very poorly written I'm sure but it works and that's all that matters.

For instance, the link you just gave me: https://docs.djangoproject.com/en/3.0/ has around 50 different hyperlinks. Sending people to that page just overwhelms people.

It's literally someone asking how to say "dog" in a language and you handing them an encyclopedia entry on dogs and all the different breeds. I don't even know where to start with all that information.
Reply
#6
So why do all views not need to render a template? What's the point of the view then? If you don't render a template, it just passes a variable?
Reply
#7
OK so the markdown is pretty easy to use. However, I still have issues. It's returning the information in HTML tags, but the HTML is not rendering on my site. Below is my code and a picture of the issue I'm having. Am I missing something?

Thanks!

def wiki(request, title):
    title = title
    entry = markdown2.markdown(util.get_entry(title))
    return render(request, "encyclopedia/wiki.html", {
        "entries": entry
    })
{% extends "encyclopedia/layout.html" %}

{% block title %}
    Encyclopedia
{% endblock %}

{% block body %}

   
            
            {{ entries }}
            
        
    
        <form
                    name="frmInfo1"
                    action="/edit"
                    method="post">{% csrf_token %}
                    <textarea name="content">{{ entries }}</textarea>
                    <input type="submit" name="edit" class="submit" value="Edit Entry">
                </form>
{% endblock %}
[Image: v.php?i=a863a0ca9e]
Reply
#8
anyone?
Reply
#9
does anyone know how to get my html rendering? thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help in finishing my scripts mohank84 5 3,751 Oct-16-2019, 04:54 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020