Cannot resolve file 'signup' - python

I am newbie to Django and i need help to solve this problem. It states that cannot resolve file'signup'.
<h3>Create your account!</h3>
<form method="post" action="/signup">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Create A Username (use only letters and numbers)" Required>
</div>

You need to have a button to submit.
<input type="submit">

Try that :
<h3>Create your account!</h3>
<form method="post" action="/signup">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Create A Username (use only letters and numbers)" Required>
</div>
<input type="submit">
</form>
Your form was not a valid one and was not submitting anything.

Related

Csrf_token error on production. but no error on localhost

I just finished my first Django project and I deployed it on Railway. It deployed successfully but my contact form gives the csrf_token error on production but doesn't give an error on localhost.
I have csrf_token tag in my contact form on my html template.
<form action="{% url 'contact' %}" method="post">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" name="message-name" placeholder="Your Name" required>
</div>
<div class="form-group">
<input class="form-control" type="email" name="message-email" placeholder="Your Email" required>
</div>
<div class="form-group">
<textarea name="message" class="form-control" id=" placeholder="Message *" rows="7" required></textarea>
</div>
<div class="form-group ">
<button type="submit" class="form-control btn btn-primary" >Send Message</button>
</div>
</form>
what should i do about this
I assume you are using Django 4.0 or above, where CSRF_TRUSTED_ORIGINS is required to be in the settings.py.
You can specify this env variable as follows.
CSRF_TRUSTED_ORIGINS = ['https://YourSite.com', 'http://YourSite.com']
You can refer here and here.

request.POST parameters are missing

Hello I am trying to pass some values from my signup form to a django view but for some reason the values are missing.
My view.py :
#csrf_exempt
def signup(request):
if request.method =='POST':
for key, value in request.POST.lists():
print(" DEBUG %s %s" % (key, value))
print(request.POST['name'],email=request.POST['email'])
return render(request, 'front_app/login.html')
My register.html :
<form class="form" method="post" action="{% url 'signup' %}" name="submitForm">
{% csrf_token %}
<input type="text" class="name" placeholder="Name" id="name" required>
<input type="email" class="email" placeholder="Email" id="email" required>
<div>
<input type="password" class="password" placeholder="Password (8 characters minimum)" id="password" minlength="8" required>
</div>
<input type="submit" class="submit" value="Register" >
</form>
and my urls.py :
urlpatterns = [
# other not related urls
path('signup', views.signup, name='signup'),
]
My problem is that when the views.signup function is running it is missing all three values that I am trying to send : name, email and password.
It is a POST request (and not GET) but the only values in the QueryDict are the "encoding" and "csrfmiddlewaretoken" values.
Any help is appreciated!
As Selcuk comment,
The request.POST data is a dict filled with the name of the input fields of the HTML form.
Try with :
<form class="form" method="post" action="{% url 'signup' %}" name="submitForm">
{% csrf_token %}
<input type="text" class="name" placeholder="Name" id="name" name="name" required>
<input type="email" class="email" placeholder="Email" id="email" name="email" required>
<div>
<input type="password" class="password" placeholder="Password (8 characters minimum)" id="password" minlength="8" name ="password" required>
</div>
<input type="submit" class="submit" value="Register" >
</form>

How do I get images to show on my django app?

On my Django app, I am trying to make an eBay type of site that lets people post listings with pictures and bid on them. Everything works except posting an image. The image won't show and I do not know if it's the HTML or the python. If you have any questions let me know.
Html code:
{% extends "auctions/layout.html" %}
{% block body %}
<div class="container">
<h2>Create listing</h2>
</div>
<div class="container">
<form action="{% url 'submit' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlInput1">Title</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Title of the lisiting..." name="title" required>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" placeholder="Description..." name="description" required></textarea>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Category</label>
<select class="form-control" id="exampleFormControlSelect1" name="category" required>
<option>Fashion</option>
<option>Tools</option>
<option>Toys</option>
<option>Electronics</option>
<option>Home accessories</option>
<option>Books</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Initial Bid</label>
<input type="number" class="form-control" id="exampleFormControlInput1" placeholder="Starting bid..." name="price" required>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Image link (optional)</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="https://blah-blah.jpg..." name="link">
</div>
<button class="btn btn-outline-info" type="submit">Submit</button>
</form>
</div>
{% endblock %}
This is the python function for making listings:
def create(request):
try:
w = Watchlist.objects.filter(user=request.user.username)
wcount=len(w)
except:
wcount=None
return render(request,"auctions/create.html",{
"wcount":wcount
})

Django2, the forms in template do not work

(only in Django 2 & 2.0.1)
if I use {{form}} it works but if I use {{form.field}} it disappears all
<form method="POST" class="post-form" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" id="salva" class="btn btn-primary" />
</form>
forms.py
class PreventivoForm(forms.ModelForm):
class Meta:
model = Preventivo
fields = ['cliente','prestazione1', 'ripetizione1', 'prestazione2', 'ripetizione2', 'prestazione3', 'ripetizione3', 'prestazione4', 'ripetizione4', 'prestazione5', 'ripetizione5']
When you use {{form}} then it gets all these value:
<tr><th><label for="id_username">Username:</label></th><td><input type="text" name="username" autofocus maxlength="254" required id="id_username" /></td></tr>
<tr><th><label for="id_password">Password:</label></th><td><input type="password" name="password" required id="id_password" /></td></tr>
But if you use {{form.username}} and {{form.password}} then:
<input type="text" name="username" autofocus maxlength="254" required id="id_username" />
<input type="password" name="password" required id="id_password" />
So you will only see the input field not label for that input field

How to post data via form HTML?

I want to post the data to the server via form. How can I do this?
<div class="panel-body">
<form role="form" action="{% url 'login' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="sender-email" class="control-label">Username:</label>
<div class="input-icon"> <i class="icon-user fa"></i>
<input id="sender-email" type="text" placeholder="Username" class="form-control email">
</div>
</div>
<div class="form-group">
<label for="user-pass" class="control-label">Password:</label>
<div class="input-icon"> <i class="icon-lock fa"></i>
<input type="password" class="form-control" placeholder="Password" id="user-pass">
</div>
</div>
<div class="form-group">
<input class="btn btn-primary btn-block" id="authenticate_user" type="submit" value="Submit">
</div>
</form>
</div>
My views.py
#csrf_exempt
def signin(request):
if request.method == 'POST':
print request.POST.get('sender-email') #prints None
username = request.POST['username'] # throws error, username not available.
password = request.POST['password']
How do I post the data without using jquery? I know of a way of posting through AJAX jquery, but dont wanna do that.
The 'name' attribute of input tags is set as keys of request.GET and request.POST.
Add name attribute to all your input tags.
<input name="username" id="sender-email" type="text" placeholder="Username" class="form-control email">
<input name="password" type="password" class="form-control" placeholder="Password" id="user-pass" >
POST data are populated using the name attribute of your input elements. Change your id attributes or add a name one accordingly. E.g:
<input type="password" class="form-control" placeholder="Password" name="user-pass">
You should also consider using a form to handle your data more easily.

Categories