0

I am working on project1 cs50w wiki, I have created a form in forms.py. it is textinput and I am trying to pass dynamic value for thistext input "{{ pagetitle }}". but it prints "{{ pagetitle }}" as it is in the textinput when rendered. this all works if I am using HTML forms but when I try Django forms I can not figure out how to pass variable to the display.

forms.py

from django import forms


class newTitle(forms.Form):
    q = forms.CharField(label='New Page Title', max_length=100, widget=forms.TextInput(
        attrs={
            'id': 'titleCheck',
            'class': 'search', 
            'type': 'text',
            'placeholder': 'Type Topic Title Here...',
            'onblur': 'blurFunction()',
            'value': '{{ pagetitle }}'
        })) 

views.py

def new_topic(request):
    newTitle_form = newTitle()
    context = {
        "newTitle_form": newTitle_form,
    }
    return render(request, "encyclopedia/CreateNewPage2.html", context)

def new_topic_t(request, entry):
    newTitle_form = newTitle()    
    return render(request, "encyclopedia/CreateNewPage2.html", {
                "newTitle_form": newTitle_form,
                "pagetitle": entry.capitalize()
            })
def myCheckFunction(request):
    searchPage = request.GET.get('q','')
    if(util.get_entry(searchPage) is not None):
        messages.info(request, 'The Page '+ searchPage + ' Already Exists, Do You Want To Change It Or Edit It!')
        return HttpResponseRedirect(reverse("new_topic_t", kwargs={'entry': searchPage}))
    else:
        return HttpResponseRedirect(reverse("new_topic_t", kwargs={'entry': searchPage}))

createNewPage2.html

{% csrf_token %}
    <div class="form-group" style="margin-right: 20%">        
        <form id="newTitle" action="{% url 'check' %}" method="GET">
            {% block newTitle %}
                {{ newTitle_form }}          
            {% endblock %}
        </form>
     </div>

<script>
        function blurFunction() {
            var newTitleText = document.getElementById("titleCheck");
            if (newTitleText.value !== null && newTitleText.value !== "") {
                document.getElementById("newTitle").requestSubmit();
            }                    
        }
    </script>

    <script>        
        window.onload = function() {
            var buttonElement = document.getElementById("changeTopic");
            var newTitleText = document.getElementById("titleCheck");
            if ((!buttonElement) && (newTitleText.value == "")) {
                document.getElementById('titleCheck').focus();
            } else if ((buttonElement) && (newTitleText.value != "")) {
                document.getElementById('changeTopic').focus();
            } else {
                document.getElementById('NewTopicText').focus();
            }
        }
    </script>

urls.py

urlpatterns = [
    path("", views.index, name="index"),
    path("save", views.save, name="save"),
    path("<str:withoutwikientry>", views.load_wiki, name="load_wiki"),
    path("wiki/random", views.random, name="random"),
    path("wiki/search", views.search, name="search"),
    path("wiki/myCheckFunction", views.myCheckFunction, name="check"),
    path("wiki/edit/<str:entry>", views.edit, name="edit"),
    path("wiki/<str:entry>", views.generate_page, name="generate_page"), 
    path("wiki/createNewPage/", views.new_topic, name="new_topic"),   
    path("wiki/createNewPage/<str:entry>", views.new_topic_t, name="new_topic_t"),
]

here is the screen shot of the page rendered.

enter image description here

  • you can use ```{{ }}``` only in the template files – Sumithran May 18 '21 at 08:40
  • 1
    You can pass your page title in view: `newTitle_form.fields["q"].widget.attrs.update({'value': entry.capitalize()})` – NKSM May 18 '21 at 11:21
  • after doing the above i am now getting NoReverseMatch at /wiki/createNewPage/asmat Reverse for 'edit' with arguments '('',)' not found. 1 pattern(s) tried: ['wiki/edit/(?P[^/]+)$'] error – Asmat Ullah Khan May 18 '21 at 13:41

1 Answers1

0

Thanks to NKSM i got it working by making the following code change

def new_topic_t(request, entry):
    newTitle_form = newTitle()
    newTitle_form.fields["q"].widget.attrs.update({'value': entry.capitalize()})
    context = {
        "newTitle_form": newTitle_form,
    }
    return render(request, "encyclopedia/CreateNewPage2.html", context)