Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Restaurant(models.Model):
- user = models.OneToOneField(
- User,
- on_delete=models.CASCADE,
- related_name='restaurant'
- )
- name = models.CharField(max_length=500)
- country = models.CharField(max_length=7)
- city = models.CharField(max_length=7)
- postal_code = models.CharField(max_length=7)
- class RestaurantForm(forms.ModelForm):
- class Meta:
- model = Restaurant
- widgets=({country: forms.HiddenInput(attrs={'id': country}),
- city: forms.HiddenInput(attrs={'id': locality}),
- postal_code: forms.HiddenInput(attrs={'id': postal_code})})
- fields = (
- "logo", "name", "cuisine", "country", "city", "postal_code",
- "address", "phone"
- )
- def restaurant_sign_up(request):
- user_form = UserForm()
- restaurant_form = RestaurantForm()
- if request.user.is_authenticated():
- if request.method == "POST":
- restaurant_form = RestaurantForm(request.POST, request.FILES)
- if restaurant_form.is_valid():
- new_restaurant = restaurant_form.save(commit=False)
- new_restaurant.user = request.user
- new_restaurant.save()
- return redirect(restaurant_order)
- else:
- if request.method == "POST":
- user_form = MemberForm(request.POST)
- restaurant_form = RestaurantForm(request.POST, request.FILES)
- if user_form.is_valid() and restaurant_form.is_valid():
- new_user = User.objects.create_user(**user_form.cleaned_data)
- new_restaurant = restaurant_form.save(commit=False)
- new_restaurant.user = new_user
- new_restaurant.save()
- login(request, authenticate(
- username=user_form.cleaned_data["username"],
- password=user_form.cleaned_data["password"]
- ))
- return redirect(restaurant_order)
- return render(request, "restaurant/sign_up.html", {
- "user_form": user_form,
- "restaurant_form": restaurant_form
- })
- <form method="POST" enctype="multipart/form-data">
- {% csrf_token %}
- {% if user.is_anonymous %}
- {% bootstrap_form user_form %}
- {% bootstrap_form restaurant_form %}
- {% else %}
- Hello, {{ request.user.username }}
- <br>Please Use This Form To Register Your Restaurant
- {% bootstrap_form restaurant_form %}
- <div id="locationField">
- <input id="autocomplete" placeholder="Enter your address"
- onFocus="geolocate()" type="text" style="height: 30px;width:100%;"></input>
- </div>
- {% endif %}
- <button type="submit" class="btn btn-pink pull-right">Sign Up</button>
- </form>
- <script>
- var placeSearch, autocomplete;
- var componentForm = {
- locality: 'long_name',
- country: 'long_name',
- postal_code: 'short_name'
- };
- function initAutocomplete() {
- autocomplete.addListener('place_changed', fillInAddress);
- }
- function fillInAddress() {
- var place = autocomplete.getPlace();
- for (var component in componentForm) {
- document.getElementById(component).value = '';
- document.getElementById(component).disabled = false;
- }
- // Get each component of the address from the place details
- // and fill the corresponding field on the form.
- for (var i = 0; i < place.address_components.length; i++) {
- var addressType = place.address_components[i].types[0];
- if (componentForm[addressType]) {
- var val = place.address_components[i][componentForm[addressType]];
- document.getElementById(addressType).value = val;
- }
- }
- }
- function geolocate() {
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(function(position) {
- var geolocation = {
- lat: position.coords.latitude,
- lng: position.coords.longitude
- };
- var circle = new google.maps.Circle({
- center: geolocation,
- radius: position.coords.accuracy
- });
- autocomplete.setBounds(circle.getBounds());
- });
- }
- }
- </script>
- class Restaurant(models.Model):
- user = models.OneToOneField(
- User,
- on_delete=models.CASCADE,
- related_name='restaurant'
- )
- name = models.CharField(max_length=500)
- class Address(models.Model):
- restaurant=models.ForeignKey(Restaurant, related_name"restaurant")
- country = models.CharField(max_length=7)
- city = models.CharField(max_length=7)
- postal_code = models.CharField(max_length=7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement