0

I'm new in django and i have this input in HTML,and i nedd to get the string typed from the user and send for my views.py:

        <form id="username_exists_form" method='GET'>
            Name: <input type="username" name="username" />
            <button type='submit'> Check </button>           
        </form> 

That's my view.py, i nedd the string replace "username":

    template_name = 'Exames.html'
    def get_context_data(self,**kwargs):
        context = super(ShowExames, self).get_context_data(**kwargs)
        exames = Examlabs.objects.filter(id_atendimento = username)
        context.update({'exames': exames})
        return context
    def get_queryset(self):
        return Examlabs.objects.all()```
1
  • 2
    Welcome to StackOverflow. You should search for some tutorials on "Django Forms" on the web. Commented Oct 28, 2021 at 13:48

1 Answer 1

1

First, create a UserForm

myapp/forms.py

class UserForm(forms.Form):
   user = forms.CharField(max_length = 100)

Inside the view.py

from myapp.forms import UserFrom

def user_form(request):   
   if request.method == "POST":
      # Get the posted form
      my_user_form = UserFrom(request.POST)
      
      if my_user_form.is_valid():
         # do anything here
         username = my_user_form.cleaned_data['username']
   else:
      my_user_form = UserFrom()
        
   return render(request, 'mypage.html', {"username" : username})

Inside mypage.html

<html>
   <body>
      <form name = "form" action = "{% url "myapp.views.user_form" %}" 
         method = "POST" >{% csrf_token %}
               <input type = "text" name = "username" />
               <button type = "submit" value = "Submit" ></button>
      </form>
   </body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

If this is helpful please upvote my solution.
Doing in that way is possible to make a filter in exame? In the code there's already in my view.

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.